Yes, mxDestroyArray() recursively de-allocates elements of structs and cells. Otherwise you could observe a memory leak.
[EDITED]
The documentation of mxDestroyArray explains clearly (e.g. in R2009a):
mxDestroyArray not only deallocates the memory occupied by the mxArray's characteristics fields [...], but also deallocates all the mxArray's associated data arrays, such as [...
And a small C-mex test function (call it test_mxDestroy.c and compile it):
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mxArray *C;
C = mxCreateCellMatrix(1, 1);
mxSetCell(C, 0, mxCreateDoubleMatrix(1, 1000000, mxREAL));
mxDestroyArray(C);
}
If the contents of the created cell is not freed implicitly, 8MB memory would be leaked in each call. Now inspect the operating systems memory manager while running:
for k = 1:1e6, test_mxDestroy; end
You will see, that the memory is not exhausted.
As James has explained already, your example does not crash accidentally only. When you try to use mat after mxDestroyArray(cell), you will encounter a crash soon. ATTENTION: Crashing the Matlab session can destroy data. So keep care, and even better keep a backup.
Best Answer