How can I free memory using pointers in mexfiles

10 ビュー (過去 30 日間)
Roger Pallarès López
Roger Pallarès López 2017 年 10 月 23 日
コメント済み: James Tursa 2017 年 10 月 24 日
Hello, I'm using a MEXfile C++ code for an optimization task. I noticed that memory is not freed at each iteration of the optimization, so after a number of iterations MATLAB crashes due to a no memory space error (RAM memory in Task Manager is completely full). As I'm new in programming MEXfiles I have low experience in this kind of memory errors. I tried to delete the memory stored in pointers but I couldn't manage to solve the problem. I thought the cause of this issue could be that pointers don't free the memory stored, but I'm not sure completely.
Here is my mexFunction code:
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *a, *b, *c, *d, *f;
int status,mrows,ncols;
/* Create a pointers to the input matrices. */
a = mxGetPr(prhs[0]);
b = mxGetPr(prhs[1]);
c = mxGetPr(prhs[2]);
d = mxGetPr(prhs[3]);
/* Get the dimensions of the input matrices. */
mrows = mxGetM(prhs[0]);
ncols = mxGetN(prhs[0]);
/* Set the output pointer to the output matrix. */
plhs[0] = mxCreateDoubleMatrix(mrows,ncols, mxREAL);
/* Create a C pointer to a copy of the output matrix. */
f = mxGetPr(plhs[0]);
/* Call the C++ subroutine. */
ID_mexfile_v2(a,b,c,d,f,mrows,ncols);
}
The C++ subroutine, is the function in which I treat all the input data with specific libraries of multibody dynamics. I thought it would be better not to make a long piece of code, but I can provide it as well if necessary. I would really appreciate any help or advice.
  1 件のコメント
James Tursa
James Tursa 2017 年 10 月 24 日
There is nothing in your posted mexFunction code that leaks memory, so any leaking must be in your ID_mexfile_v2 function.

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Compiler についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by