Xueming
Posts:
4
Registered:
12/4/12
|
|
Re: matlab/mex function doesn't free memory
Posted:
Dec 5, 2012 5:54 AM
|
|
"James Tursa" wrote in message <k9lhsc$b0h$1@newscl01ah.mathworks.com>... > "Xueming " <emilyhexueming@hotmail.com> wrote in message <k9l55g$j9c$1@newscl01ah.mathworks.com>... > > > Currently I meet exactly the same problem. I wrote a function in C which returns mxArray* to Matlab, and this function is called in a shared library as well, and it will be called continuously. To free memory which occupied by return mxArray I wrote a function ( using mxDestroyArray), however, Matlab crashed, and seems it cannot find the location of returned mxArray. Is that becaues Matlab has it own garbage collection and after every call of function, it frees the memory occupied by returned mxArray? > > Have you figured this issue out? I will appreciate a lot if you can give me any hint:) > > In general, you should *not* be using mxDestroyArray on anything that is being returned to MATLAB. That will invalidate the data in the mxArray and cause MATLAB to crash. > > James Tursa
Hi James, Now I have two issues, 1) I create a C function, which will be called by Matlab, and when everytime it's called, it create a mxArray and return this pointer to Matlab. With called more and more times, it creates more and more mxArray. I guess the memory must be significantly occupied. I'm thinking if we can mxDestory a mxArray (by writing another function) as soon as it's returned to Matlab, so that we only allocate memory for current mxArray.
2)How about inside C code, if declaring a global mxArray*, how to make this global pointer point to a mxArray which is returned by a C function. I tried to declare a global mxArray*, and set it point to a mxArray and returned to Matlab. This global mxArray* is used in other functions. But it seems in other functions, the global mxArray pointer could not be found where it points to. I got some strange result. Could anyone tell me the reason. Here is a simple code. if run this code, calllib('mxArrayTest', 'create'), calllib('mxArrayTest','test'); the result of retruing mxCell1 and mxCell_g are completely different. The free_mxelem(mxCell_g) works pretty well just following mxCell_g=mxCell1, however, outside the "create" function, the free_mxelem() shows pretty strange result. I assume this is because after C returns mxArray to Matlab, memory of mxArray will be free automatically.
here is my code: /////////////////mxArrayTest.c #include "mex.h" /* only needed because of mexFunction below and mexPrintf */ #include <math.h> #include "mxArrayTest.h" #include "string.h"
/*******************function declare***********************************/ #define mxNULL mxCreateDoubleMatrix(0,0,mxREAL) mxArray *mxCell_g;
/*type check*/
void type_check(void) { int num; int i; mxCell_g=mxNULL; num=mxGetN(mxCell_g);
if(mxIsEmpty(mxCell_g)) { mexPrintf("Empty"); } else { for(i=0;i<num;i++) { mexPrintf("type is: %s",mxGetClassName(mxCell_g)); } } }
/********************get element************************************/ void getelement(int pos,mxArray* mxCell_g) { if(mxIsEmpty(mxCell_g)) { mexPrintf("No element!\n"); }
else if(mxIsCell(mxGetCell(mxCell_g,pos))) { mexPrintf("This element is a cell!\n "); }
else { mexPrintf("This elem is other type!\n"); }
} /********************************************************/ int free_mxelem(mxArray* mxelem)
{ if(mxIsEmpty(mxelem)) { mexWarnMsgTxt("Empty Element!\n"); return 0; } else if(mxIsChar(mxelem)||mxIsDouble(mxelem)) { mxDestroyArray(mxelem); mexPrintf("Single Successful!\n"); return 1; } // else if(mxIsCell(mxelem)) else if(mxIsCell(mxelem)) { int n;//number of element int i;//counter n=mxGetN(mxelem); mexPrintf("Free Cell!\n"); for(i=0;i<n;i++) {
free_mxelem(mxGetCell(mxelem,i));
} mxDestroyArray(mxelem); mexPrintf("Cell Successful!\n"); return 1; } return 0; }
/**************test******************************/
DLL_EXPORT mxArray* create(void) { mxArray *mxDouble; mxArray *mxCell; mxArray *mxCell1; mxArray *mxString;
double *a; char *str; mxDouble=mxCreateDoubleMatrix(1,2,mxREAL); a=mxGetPr(mxDouble); a[0]=1; a[1]=2;
str=(char*)malloc(sizeof(char)*20); strcpy(str,"mxArray Test"); mxString=mxCreateString(str);
mxCell=mxCreateCellMatrix(1,2); mxSetCell(mxCell,0,mxDouble); mxSetCell(mxCell,1,mxString);
mxCell1=mxCreateCellMatrix(1,2); mxSetCell(mxCell1,0,mxCell); mxSetCell(mxCell1,1,mxString);
mxCell_g=mxCell1; // free_mxelem(mxCell_g); mexPrintf("is this empty? %d\nClass: %s\n",mxIsEmpty(mxCell_g),mxGetClassName(mxCell_g)); // return mxCell_g; return mxCell1; }
DLL_EXPORT void test(void) { int free_mxelem(mxArray* mxelem); mexPrintf("is this empty? %d\n%s\n",mxIsEmpty(mxCell_g),mxGetClassName(mxCell_g)); free_mxelem(mxCell_g);
}
////////////////////////mxArrayTest.h #ifndef _MXARRAYTEST_H_ #define _MXARRAYTEST_H_ #define DLL_EXPORT __declspec(dllexport)
#ifdef __cplusplus extern "C" { #endif
#include "mex.h"
//free_element //DLL_EXPORT void free_elem(void);
/*test*/ DLL_EXPORT void test(void); DLL_EXPORT mxArray* create(void); DLL_EXPORT void type_check(void);
#ifdef __cplusplus } #endif
#endif
/////////////////end
If this piece of code and this message are messy for, I do apologize, since I'm a really beginer with this.
Emily
|
|