MATLAB: MexCallMATLAB Memory Leak / mxDestroyArray / C++ Crashing

cmemorymex

I'm having trouble using mexCallMATLAB in the code below. I'm successfully feeding in the x,y,z coordinates and corresponding normal vectors into the Matlab function (as I can stop and debug the file check_occ_curve.m). But I think something in my mxDestroyArray calls is causing a crash.
My chain of reasoning here is basically to copy all the points into a new array x_p, because the points stored in the point cloud model are integers (model.pts[i].x is the x coordinate of the ith point, similarly for y and z, and model.normals[i].x is the x component of the normal vector at point i). Then I fill in pts_and_normals, which contains (the pointers to) three mxArrays, where I store the pointers for this new data. Then I want to destroy the arrays once I'm done with the mexCallMATLAB call, but I'm reasonably sure that the crash is occurring at an mxDestroyArray or because of some sort of memory leak on something that I should be destroying but am not.
Comment 1: When I run the following WITH the line mxDestroyArray(ppLhs[0]) and mxDestroyArray(ppLhs[1]), I get a crash (and one member of the stack trace is: "…mx_array_api31try_nonrecursive_mxDestroyArrayEP11mxArray_tag+00000015.")
Comment 2: But when I remove the lines mxDestroyArray(ppLhs[0]) and mxDestroyArray(ppLhs[1]) and add mxSetData(pts_and_normals[0], NULL); mxSetData(pts_and_normals[1], NULL); mxSetData(pts_and_normals[2], NULL); the stack trace is more mysterious, the line after the call to occ_curve.cpp (which is the function that calls this method) in the stack trace is "<unknown-module>+00000000."
When I remove the mxSetData and mxDestroyArray(ppLhs[0]),… . The stack trace again contains: "try_nonrecursive_mxDestroyArrayEP11mxArray_tag+00000015."
Does anyone have any ideas how I can fix this? The crash MUST be happening AFTER the mexCallMATLAB line (because I can stop that .m file in the Matlab debugger), so I think it's either a memory leak/problem (much more likely) or some sort of problem in the way I'm assigning or saving the output data from mexCallMATLAB.

Best Answer

Some comments:
1) The signature for mxCreateNumericArray shows the dims argument as being mwSize, not mwSignedIndex. So this could be a mismatch causing problems. Change these lines to:
mwSize dims[2];
:
mwSize dims2[2];
2) I don't see R or n1 defined anywhere. Are they global?
3) First you create three mxArray's and stuff them into an array:
pts_and_normals[0] = mxCreateDoubleMatrix(1,1,mxREAL);
pts_and_normals[1] = mxCreateDoubleMatrix(1,1,mxREAL);
pts_and_normals[2] = mxCreateDoubleMatrix(1,1,mxREAL);
Then you almost immediately replace this with something else, which LEAKS the original mxArray memory above:
pts_and_normals[0] = x_p;
pts_and_normals[1] = x_n;
pts_and_normals[2] = xy_size_of_image;
Then at the end of your code you destroy the same mxArray's twice, since they are pointing to the same thing:
mxDestroyArray(pts_and_normals[0]);
mxDestroyArray(pts_and_normals[1]);
mxDestroyArray(pts_and_normals[2]);
mxDestroyArray(x_p);
mxDestroyArray(x_n);
mxDestroyArray(xy_size_of_image);
That 2nd set of mxDestroyArray calls will crash MATLAB since they were already destroyed by the first three lines.
4) This line looks very strange to me and is likely an error:
oc_ind[i]= (int) *(mxGetPr(ppLhs[1]) + i * sizeof(double));
The result of the mxGetPr call is of type "pointer to double". Adding i to it will automatically do pointer arithmetic (i.e., add 8 to the address). Multiplying it by sizeof(double) is probably not what you intended since that would skip every 8 elements and I am assuming run off the end of the array causing a crash. I am guessing this is what you want:
oc_ind[i]= (int) *(mxGetPr(ppLhs[1]) + i);
5) You haven't posted enough code for me to double check your memcpy stuff, since I have no way to determine the types involved.
Related Question