crash when clearing or re-writing upon return from mex C file

4 Ansichten (letzte 30 Tage)
Jeff
Jeff am 24 Mär. 2012
This issue pertains to MATLAB version 7.11.0.548 (R2010b), 32-bit (win32).
I have a mex C file that creates as part of its output an mxNumericArray (360x640x3).
Upon returning from the mex file, the output variables are successfully created in the MATLAB workspace, and I can look at the returned array using image( ). If I try to run the mex routine a second time, I get a segmentation fault and a MATLAB crash. If I try to clear the returned variable in the MATLAB workspace, I also get a crash (reported as 'abnormal termination' rather than explicitly a segmentation fault).
The mex routine seems to work fine, no runtime errors until the SECOND return. The results are the same if the MATLAB workspace variables populated by the mex return are declared previously (or not) in the m-file.
The error happens when the mex routine is called a SECOND time, or if I try to clear the MATLAB workspace variable in question.
On occasion, typing 'whos' can also cause an abnormal termination after calling the mex routine...but not always. I'm not sure of the dependency involved in that sporadic failure.
My guess is that something weird is happening to the MATLAB workspace array upon the mex routine's return.
MORE DETAILED INFO:
(Apologies...this editor is doing weird things with carriage returns, so some of the lines look like they run together.)
The calling syntax for the mex routine SpaceTimeErrors( ), if helpful in this investigation, is as follows:
[MCC, CCC, confusionstats, confusionmaskRGB] =
SpaceTimeErrors( xc1, yc1, xr1, yr1, xc2, yc2, xr2, yr2, w, h, f);
All of the input arguments are double scalars, and the output variables are as follows:
MCC double 1x1;
CCC double 1x1;
confusionstats double 1x4;
confusionmaskRGB double 360x640x3;
The last one is the troubled child.
Within the mex C file's gateway function, the declaration for this array is as follows:
mwSize ndimsconfusionmaskRGB = 3;
mwSize *dimsconfusionmaskRGB;
double *confusionmaskRGB;
dimsconfusionmaskRGB = (mwSize *) mxMalloc (3 * sizeof(mwSize));
dimsconfusionmaskRGB[0] = h;
dimsconfusionmaskRGB[1] = w;
dimsconfusionmaskRGB[2] = 3;
plhs[3] = mxCreateNumericArray(ndimsconfusionmaskRGB,dimsconfusionmaskRGB,
mxDOUBLE_CLASS,mxREAL);
confusionmaskRGB = mxGetPr(plhs[3]);
...
mxDestroyArray( confusionmaskRGB );
Addressing this array in the computational routine is as follows, e.g.:
confusionmaskRGB[j + i*(int)h] = 0;
confusionmaskRGB[j + i*(int)h + 1*(int)h*(int)w] = 0;
confusionmaskRGB[j + i*(int)h + 2*(int)h*(int)w] = 1;
I NEVER get any runtime errors DURING calls this mex routine...the problems are all back in the MATLAB workspace / runtime.
Thanks...
  1 Kommentar
Jan
Jan am 25 Mär. 2012
I've formatted the code for you. Simply mark it and hit the "{} Code" button. Follow the "Markup help" link to learn more.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 25 Mär. 2012
confusionmaskRGB = mxGetPr(plhs[3]);
Now confusionmaskRGB is a pointer to a double array.
mxDestroyArray( confusionmaskRGB );
mxDestroyArray destroys an mxArray, but it receives a pointer to a double array. If this does not crash inside the Mex function, you are simply lucky.
  3 Kommentare
Jan
Jan am 25 Mär. 2012
I do not understand what you changed from double to mxArray. Omit the mxDestroyArray command.
Jeff
Jeff am 26 Mär. 2012
Aha! Yes, it was the mxDestroyArray (and when I declared the variable as a matrix rather than an n-dim array, mxFree) that was causing the trouble.
The allocation or reference from within the MATLAB workspace was apparently broken by releasing these variables from within the mex file.
Thank you for your help!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Write C Functions Callable from MATLAB (MEX Files) finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by