MATLAB: How to “free” or “destroy” pointer array of mxArray

destroyMATLABmexmxarraypointer

Hi,
I am using mexCallMATLAB in mex. So I need to construct pointer array for multi input or output. Like the below tmp variable. I would like to know how to free it.
mxArray *tmp[3];
mexCallMATLAB(3,tmp,1,A,"find");
% must I do it in this way
mxDestroyArray(tmp[0]);
mxDestroyArray(tmp[1]);
mxDestroyArray(tmp[2]);
% or
mxDestroyArray(tmp);

Best Answer

You must do each one. So
mxDestroyArray(tmp[0]);
mxDestroyArray(tmp[1]);
mxDestroyArray(tmp[2]);
or you could put these in a loop.
Doing this
mxDestroyArray(tmp);
would likely result in a seg fault, since tmp does not point to any dynamically allocated mxArray. In particular, it would definitely not destroy the three mxArrays you want destroyed.