Page 1 of 1

Problems with memory using mexFiles and OpenSim API

Posted: Mon Oct 23, 2017 9:03 am
by rogerpallares
Hi all,
I'm using a MEXfile C++ code for an optimization task using GPOPS in MATLAB and programming OpenSim libraries with C++. 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 completely sure. Also, I thought about being a problem of an OpenSim function or maybe a bad compilation of the mexFunction.
If anyone has any idea of how I can solve it or any experience of this issue, I would really appreciate any help or advice.

I attach the mexFunction code, I thought it would be better not to make a long piece of code, but I can provide the subroutine function with all the OpenSim functions as well if necessary.

Code: Select all

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);    
  }