This is because you are trying to alter memory that will be allocated and destroyed by MATLAB. The “mxSetPr” function should be use only to replace the initial real values with new ones.
Instead use:
Marshal.Copy(dblTmpArray, 0, Mathworks.mxGetPr(mxPtrTmp), intRows * intCols);
Even a simple function like
#include "mex.h"
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
double* d = (double*)malloc(100*8);
mxArray* a = mxCreateNumericMatrix( 1, 100, mxDOUBLE_CLASS, mxREAL );
if(a)
{
mxSetPr(a, d);
}
mexPrintf("end\n");
}
will cause the same.
See the following documentation for additional information on memory management :
Best Answer