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