mex crashes when adding input AND calling function

2 次查看(过去 30 天)
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
  1 个评论
Jim Hokanson
Jim Hokanson 2014-4-1
Also, perhaps not surprisingly, the error seems to be happening on cleanup. Print statements after the second result will run.

请先登录,再进行评论。

采纳的回答

Jim Hokanson
Jim Hokanson 2014-4-1
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.
  2 个评论
James Tursa
James Tursa 2014-4-1
编辑:James Tursa 2014-4-1
You don't show enough code for me to be certain what is going on with "option" (I don't see where it is commented), but in your Answer code the difference is this:
--------------
Crashing Code:
long *nRecords;
result = ADI_GetNumberOfRecords(fileH,nRecords)
In the above code, nRecords is an uninitialized pointer. Its value is garbage and if ADI_GetNumberOfRecords is using that pointer then it will be accessing invalid memory and crashes are to be expected. The correct way to do this if you insist on using a pointer variable would be:
long *nRecords;
nRecords = (long *) mxMalloc(1 * sizeof(*nRecords));
result = ADI_GetNumberOfRecords(fileH,nRecords)
// use nRecords
mxFree(nRecords);
-------------
OK Code:
long nRecords;
result = ADI_GetNumberOfRecords(fileH,&nRecords)
In the above code, nRecords is in fact "allocated" (it is a local variable with memory behind it), so &nRecords is a pointer that points to valid memory. Not surprising that you don't get a crash in this case.
So, based on the behavior you describe (and the name of the routine) I would strongly suspect that ADI_GetNumberOfRecords writes to the address behind the 2nd argument pointer, and your crashing code is just a coding error on your part (passing an invalid pointer). It has nothing to do with this being a 3rd party library.
Jim Hokanson
Jim Hokanson 2014-4-6
Thanks James! The difference between allocating memory for the pointer and allocating memory for the variable makes sense.
I guess the confusion for me really comes in with who is responsible for allocating the memory. Since it was a 3rd party library I couldn't see that they were assuming the variable memory had been allocated, but perhaps variable initialization is best practice (and expected) anyway.
It is still a bit surprising that Matlab seemed fine with things until I used the option variable. What is shown above is the entirety of the code. I hadn't actually gotten around to using option yet.
Anyways, thanks for the clarification.
Jim

请先登录,再进行评论。

更多回答(0 个)

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by