MATLAB: Crash when clearing or re-writing upon return from mex C file

cclearmexreturnwhos

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…

Best Answer

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.