Why when I add mxDestroyArray(myArray) to my code MATLAB crashes?
3 views (last 30 days)
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?
Kaustubha Govind on 24 Oct 2012
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?
Andrew Stamps on 15 Feb 2013
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.