Also, perhaps not surprisingly, the error seems to be happening on cleanup. Print statements after the second result will run.
mex crashes when adding input AND calling function
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Jim Hokanson
le 1 Avr 2014
Commenté : Jim Hokanson
le 6 Avr 2014
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
Réponse acceptée
Jim Hokanson
le 1 Avr 2014
2 commentaires
James Tursa
le 1 Avr 2014
Modifié(e) : James Tursa
le 1 Avr 2014
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.
Plus de réponses (0)
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!