MATLAB: Do I receive the error “The pointer passed to ‘vector_check’ is invalid” error when using mxSetPr

MATLABmxsetprvector_check

I am trying to use mxSetPr inside a C# application and it returns the following error:
The pointer passed to 'vector_check' is invalid

Best Answer

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 :