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

조회 수: 4 (최근 30일)
Jeff
Jeff 2012년 3월 24일
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
Jan
Jan 2012년 3월 25일
I've formatted the code for you. Simply mark it and hit the "{} Code" button. Follow the "Markup help" link to learn more.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2012년 3월 25일
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
Jan
Jan 2012년 3월 25일
I do not understand what you changed from double to mxArray. Omit the mxDestroyArray command.
Jeff
Jeff 2012년 3월 26일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by