MATLAB: MxGetPr crashing for no reason

mex mxgetpr

Hello,
I am trying to create a mex file which will modify the contents of the array in plhs, but whenever I run the code it crashes when I try to assign the adress of a double* to plhs[0]. Below is my code…
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int l = (int)mxGetScalar(prhs[0]);
int nslc = (int)mxGetScalar(prhs[1]);
double *spc = (double*)mxGetData(prhs[2]);
int Tnpx_w = (int)mxGetScalar(prhs[3]);
int Tnpx_h = (int)mxGetScalar(prhs[4]);
double *Tpx_w0 = (double*)mxGetData(prhs[5]);
double *Tpx_h0 = (double*)mxGetData(prhs[6]);
int Inpx_w = (int)mxGetScalar(prhs[7]);
int Inpx_h = (int)mxGetScalar(prhs[8]);
double *Ipx_w = (double*)mxGetData(prhs[9]);
double *Ipx_h = (double*)mxGetData(prhs[10]);
double *a = (double*)mxGetData(prhs[11]);
double *D = (double*)mxGetData(prhs[12]);
double alpha = (double)mxGetScalar(prhs[13]);
double OMEGA = (double)mxGetScalar(prhs[14]);
double *W = (double*)mxGetData(prhs[15]);
double *img = (double*)mxGetData(prhs[16]);
int dims[3] = {Tnpx_h,Tnpx_w,nslc};
int s,m,n;
double L, Tpx_w, Tpx_h;
double TposX, TposY, TposZ;
double SposX, SposY, SposZ;
double IX, IY;
int IXi, IYi;
double VX, VY, VZ;
double *ts_posX = (double*)mxMalloc(Tnpx_w*sizeof(double));
double *ts_posY = (double*)mxMalloc(Tnpx_h*sizeof(double));
double *img0;
// plhs[0] = mxCreateNumericArray(3, dims, mxDOUBLE_CLASS, mxREAL);
printf("test0");
img0 = mxGetPr(plhs[0]);
printf("test1");
//begin code ...
}
An example of how I call this in matlab is…
img0=Pdts(l,nslices,spacing,Tnpx_w,Tnpx_h,Tpx_w0,Tpx_h0,Inpx_w,Inpx_h,Ipx_w,Ipx_h,a,D,alpha,OMEGA,W(count),img);
this is in a large loop where img0 is a 3d array initially set to all zeros. Each call should update the array img0 to add in new values to what is in img0 when Pdts is called. The c code compiles fine, but it is crashing on the line
img0 = mxGetPr(plhs[0]);
I know this because the first printf statement will execute, but the second will not. If I add in the commented out line
plhs[0] = mxCreateNumericArray(3, dims, mxDOUBLE_CLASS, mxREAL);
it will not crash and run. However, each time I run it I reallocate a new space within the memory and so img0 does not retain the values from previous calls to Pdts. Could anyone give me some help with getting this working? Thanks

Best Answer

If you want to operate on a calling workspace variable in-place iteratively, you will have to pass that variable into the mex routine each iteration. In other words, you need to create this variable at your m-file level and pass it into your mex routine, NOT create this variable as a plhs[0]. E.g., if you pass it in as the first argument, then you will be using img0 = mxGetPr(prhs[0]) each time inside the mex routine. There will be no explicit output at the m-file level, nor should you create any plhs[0] inside the mex routine. E.g., the m-file calling syntax would be:
Allocate img0 once, then inside your loop do this:
Pdts(img0,l,nslices,spacing,Tnpx_w,Tnpx_h,Tpx_w0,Tpx_h0,Inpx_w,Inpx_h,Ipx_w,Ipx_h,a,D,alpha,OMEGA,W(count),img);
and inside the mex routine Pdts you would be modifying prhs[0] in-place each time.
CAUTION: This in-place modification violates the strict const assumption of the prhs inputs. If img0 is shared with any other variable at the m-file level you will inadvertantly modify that other variable as well and possibly get incorrect results from your code.
Alternatively to operating on the variable in-place, you can duplicate it each time if you are willing to accept the extra data copying involved. E.g.,
Allocate img0 once, then inside your loop do this:
img0 = Pdts(img0,l,nslices,spacing,Tnpx_w,Tnpx_h,Tpx_w0,Tpx_h0,Inpx_w,Inpx_h,Ipx_w,Ipx_h,a,D,alpha,OMEGA,W(count),img);
And then inside the mex routine have this statement to create the plhs[0]:
plhs[0] = mxDuplicateArray(prhs[0]);
img0 = mxGetPr(plhs[0]);
As a side note, you should be using mwSize for dims, not int. E.g.,
mwSize dims[3] = {Tnpx_h,Tnpx_w,nslc};
Related Question