How can I free memory using pointers in mexfiles
10 views (last 30 days)
Show older comments
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 Comment
James Tursa
on 24 Oct 2017
There is nothing in your posted mexFunction code that leaks memory, so any leaking must be in your ID_mexfile_v2 function.
Answers (0)
See Also
Categories
Find more on MATLAB Compiler in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!