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 CenterFile Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by