Effacer les filtres
Effacer les filtres

Does mxDestroyArray() recursively de-allocate elements of structs and cells?

2 vues (au cours des 30 derniers jours)
Szabolcs
Szabolcs le 16 Fév 2013
The question is in the title. Is is about the C matrix library interface.
To explain in more detail, suppose that I create a 1 by 1 cell using mxCreateCellArray(), then create a numeric matrix using mxCreateNumericArray() and set it as the only element of the cell. Now will calling mxDestroyArray() on the cell destroy the numeric array as well, in one go? Or do I need to call it separately for the elements of the cell, then just the cell? I am hoping for the latter, as this is more reasonable for complex manipulations.
The documentation is ambiguous on this point.
  2 commentaires
Szabolcs
Szabolcs le 16 Fév 2013
Modifié(e) : Szabolcs le 17 Fév 2013
Jan
Jan le 17 Fév 2013
@Syabolcs: Thanks for mentioning the cross-posting. This is a good example for others. +1
You can omit the memset(), because the memory is initialized already.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 17 Fév 2013
Modifié(e) : Jan le 17 Fév 2013
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 [...], fields of structure arrays, and cells of cell arrays.
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.
  13 commentaires
James Tursa
James Tursa le 17 Fév 2013
Modifié(e) : James Tursa le 18 Fév 2013
@Jan: Technical side note ... for a normal return from a mex routine, shared data copies of the plhs[] variables are what are actually returned, not the plhs[] variables themselves. Then everything on the garbage collection list is destroyed, including the plhs[] variables which are in fact on the garbage collection list (except for persistent variables such as prhs[] or using mexMakeArrayPersistent). For an error return, the only difference is that shared data copies of the plhs[] variables are not made, only the garbage collection (including the plhs[] variables) takes place.
Jan
Jan le 17 Fév 2013
Thanks, James, to explain this detail, which is surprising for me.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by