MATLAB: How to make memory persistent between calls to a MEX-file in MATLAB

MATLABmemorymexmexmakearraypersistentmexmakememorypersistentpersistent

I would like to know if memory allocated with mxCalloc remains persistent between calls to a MEX-file.

Best Answer

By default, in a MEX-file, mxCalloc generates non-persistent mxCalloc data. In other words, the memory management facility automatically de-allocates the memory as soon as the MEX-file ends. If you want the memory to persist after the MEX-file completes, call mexMakeMemoryPersistent after calling mxCalloc. If you write a MEX-file with persistent memory, be sure to register a mexAtExit function to free allocated memory in the event your MEX-file is cleared.
For example:
// test.c
#include <stdlib.h>
#include "mex.h"
static double *myarray = NULL;
double *pr;
void exitFcn() {
if (myarray != NULL)
mxFree(myarray);
}
void mexFunction(int nlhs,mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
if (nrhs<1 | mxIsChar(prhs[0]))
mexErrMsgTxt("Must have one non-string input");
if (myarray==NULL) {
/* since myarray is initialized to NULL, we know
this is the first call of the MEX-function
after it was loaded. Therefore, we should
set up myarray and the exit function. */
/* Allocate array. Use mexMackMemoryPersistent to make the allocated memory persistent in subsequent calls*/
mexPrintf("First call to MEX-file\n");
myarray = mxCalloc(1,8);
mexMakeMemoryPersistent(myarray);
mexAtExit(exitFcn);
}
mexPrintf("Old string was '%f'.\n",myarray[0]);
pr = mxGetPr(prhs[0]);
mexPrintf("New string is '%f'.\n",pr[0]);
memcpy((char*)myarray,(char*)mxGetPr(prhs[0]), sizeof(double));
}
You can compile this MEX-file with the following command:
mex test.c
To run the MEX-file, pass in a single number. For example:
test(3)
Repetitive calls to the test MEX-file will show that the previous input is stored between calls. To clear the MEX-file from memory, use the command:
clear mex
For more information on persistent variables in MEX-files, refer to the relevent documentation by entering the following at the MATLAB prompt:
doc mexMakeArrayPersistent
and
doc mexMakeMemoryPersistent