Why when I add mxDestroyArray(myArray) to my code MATLAB crashes?

조회 수: 2 (최근 30일)
AP
AP 2012년 10월 24일
Dear All,
I have a problem with using mxDestroyArray. When I add it to my code, MATLAB crashes. Shouldn't I destroy any mxArray that I declared in my code just to avoid memory leak? Shouldn't each mxArray *myArr correspond to a mxDestroyArray(myArr) when the array is no longer needed?
Thanks,
Ahmd
  댓글 수: 1
James Tursa
James Tursa 2012년 10월 24일
Please post the offending code (or a small subset of it that reproduces the problem) so we can comment on it.

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

답변 (2개)

Kaustubha Govind
Kaustubha Govind 2012년 10월 24일
You should only call mxDestroyArray on locally created mxArrays that are not being returned in plhs. Are you deleting mxArrays that are present in either prhs or plhs?
  댓글 수: 2
AP
AP 2012년 10월 24일
This is exactly what I do. Actually, I don't delete mxArrays returned by plhs. I only delete locally created ones.
Kaustubha Govind
Kaustubha Govind 2012년 10월 25일
Okay. In that case, I would second James Tursa's suggestion to post the offending code.

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


Andrew Stamps
Andrew Stamps 2013년 2월 15일
Does your mxArray happen to contain a Java object? I recently discovered the hard way that calling mxDestroyArray() on an mxArray that contains a Java object will cause a crash, even if you otherwise should call mxDestroyArray().
If you clear the pointer to the Java object before calling mxDestroyArray(), then it doesn't seem to crash, although I don't know if there are other ramifications.
e.g.:
mxSetData(myArr, NULL);
mxDestroyArray(myArr);
  댓글 수: 2
James Tursa
James Tursa 2013년 2월 15일
Can you post an example of an mxArray that contains a Java object that causes a crash?
Andrew Stamps
Andrew Stamps 2013년 2월 16일
I encountered this when calling a C shared library generated by the MATLAB Compiler, so maybe this doesn't apply to mex functions as well, but here's a sample function where the error occurs:
static long getNumberOfStreams(const INTEGER instanceHandle)
{
long iLength;
mxArray *numStreams = NULL;
mxArray *streamArray = NULL;
if (!mlfGetStreams(2, &streamArray,
&numStreams, domArray[instanceHandle])) {
return 0;
}
iLength = (long) *(mxGetPr(numStreams));
mxDestroyArray(streamArray); // Why does this cause a crash?
mxDestroyArray(numStreams);
return (iLength > 0) ? iLength : 0L;
}
In its intended use, the input array domArray[instanceHandle] is of class org.apache.xerces.dom.DeferredDocumentImpl and is held in global memory. I am not using the first output argument from the function, streamArray, but it should hold an object of class org.apache.xerces.dom.DeepNodeListImpl. The second output argument is a standard scalar double. If I call mxDestroyArray on streamArray without mxSetData(streamArray, NULL) first, then I get a segmentation fault.
Thanks.

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

카테고리

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