MATLAB: Mex crashes when adding input AND calling function

crashMATLABmex

My mex file crashes only when I provide an input AND call a function. I'm having a hard time figuring out what is actually going on. Here is a shortened version of the code that reproduces the behavior.
Definitions of the library I am calling can be found at:
I can include them here if it turns out to be relevant.
Code from void mex
double *option;
long *nRecords;
int *x, *y;
int file_handle_value;
wchar_t *path = L"MY_PATH";
ADIResultCode result;
ADI_FileHandle fileH(0); //Pointer to ADI_FileHandle__
option = mxGetPr(prhs[0]);
result = ADI_OpenFile(path, &fileH, kOpenFileForReadOnly);
//printf("Result: [s[%d]]
//printf("Result: %x\n",result);
if (result == 0)
{
printf("unused: %d\n",fileH->unused);
//Size 4 - not sure if this will change to 8 ...
//Might need to have a check ...
//printf("pointer size: %d\n",sizeof(fileH));
printf("pointer_value: %d\n",fileH);
}
//Uncommenting this call crashes Matlab, but only if option line is uncommented as well
//result = ADI_GetNumberOfRecords(fileH,nRecords);
Call line:
sdk_test(0) %Name of the mex file
Crashes when option AND second result are both uncommented, but not one or the other

Best Answer

The problem goes away when I predefine nRecords and change it from a pointer to a value.
What's not clear to me is why the crash happens only after I start working with inputs (outputs too??) in the mex entry function.
So instead of:
long *nRecords;
result = ADI_GetNumberOfRecords(fileH,nRecords)
I do:
long nRecords;
result = ADI_GetNumberOfRecords(fileH,&nRecords)
This in general is probably good practice???; especially when passing values to a 3rd party library.
Related Question