"Bruce Elliott" <
bruce....@jhuapl.nospam.edu> wrote in message <lbu6jr$8bl$
1...@newscl01ah.mathworks.com>...
Your posts continue to drive home the point that I need to release my mex guide sooner rather than later. The MATLAB doc on this is inadequate and my mex guide adresses all of these issues.
To answer your specific question regarding mxFree, it should only be used on pointers that are returned by MATLAB API functions that dynamically allocate raw memory (not mxArray variables). Those functions that work with mxFree are:
mxCalloc
mxMalloc
mxRealloc
mxArrayToString
Functions that get pointers that are part of an mxArray should *NEVER* be passed to mxFree because that would invalidate and corrupt the mxArray (a data pointer pointing to invalid memory). E.g.,
mxGetData
mxGetImagData
mxGetPr --> equivalent to (double *)mxGetData
mxGetPi --> equivalent to (double *)mxGetImagData
mxGetChars --> equivalent to (mxChar *)mxGetData
mxGetLogicals --> equivalent to (mxLogical *)mxGetData
mxGetIr
mxGetJc
mxGetFieldNameByNumber
mxGetDimensions
You can detach the data pointer from the mxArray first and *THEN* use mxFree on it, but this typically isn't done so I won't post the details here (you need to clean up the data pointer and dimensions of the mxArray etc).
Similarly, functions that get cell or struct mxArray pointers should not be destroyed with mxDestroyArray since that would invalidate the cell or struct they are part of (pointers pointing to invalid memory). This is because they get the actual pointer from the cell or struct, not a deep copy of it. So don't mxDestroyArray the result of these function calls:
mxGetCell
mxGetField
mxGetFieldByNumber
But for new classdef objects, the only function you have to get the property returns a deep data copy (ugh):
mxGetProperty
So in this particular case, you do need to mxDestroyArray the result of the call (or you can rely on garbage collection to free it if you are lazy).
James Tursa