Mex file crash after second run.

18 views (last 30 days)
Jon
Jon on 11 Jan 2012
Hi there,
I just wrote my first mex file and am finding my matlab crashes the second time I try to run my compiled mex file. The only way I can overcome this issue is by closing matlab and reopening it before I run the mex file again. I would most appreciate it if someone could please help me overcome this issue.
Info on code: This file was made to pass in a 3D image volume from matlab, convert the image vector back into a 3D matrix after it is passed into the mex file, and then output the image as a vector after I have processed the data. Below I have my script that I use to load the 3D mri test data set from matlab, compile a mex file, and run the mex file and the actual .cpp file I created. Just note the mri volume dimension was converted from a 4D matrix into a (128 X 128 X 27) matrix and they were hard coded into my code. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Script File:
clear all close all clc
load mri I(:,:,:) = double(D(:,:,1,:));
mex Image.cpp
s = Image(I);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% .Cpp file #include mex.h #include math.h
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { mwSize r,c, i,j,k, n; mwSize Ndims; const mwSize *dims;
dims = mxGetDimensions(prhs[0]);
Ndims = mxGetNumberOfDimensions(prhs[0]);
/* get size of the matrix */
r = mxGetM(prhs[0]);
c = mxGetN(prhs[0]);
double *input;
double output[128*128*27];
double temp[128][128][27];
/* get pointer to data */
input = mxGetPr(prhs[0]);
/* access matrix using row/column indices */
mwSize count = 0;
for (i=0; i<128; i++) {
for (j=0; j<128; j++) {
for (k=0; k<27; k++) {
temp[i][j][k] = input[count] * input[count];
count++;
}
}
}
count = 0;
for (i=0; i<128; i++) {
for (j=0; j<128; j++) {
for (k=0; k<27; k++) {
output[count] = temp[i][j][k];
count++;
}
}
}
/* Create a matrix for the return argument */
plhs[0] = mxCreateNumericArray(Ndims,dims,mxDOUBLE_CLASS,mxREAL);
mxSetPr(plhs[0], output);
return;
}

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 11 Jan 2012
Hi Jon,
the problem is, you use the temporary memory of output in mxSetPr. mxSetPr does not copy but only set's the pointer. At the end of your mex function the variable output is destroyed (and therefore the data of plhs[0] as well!).
What you have to do: move the mxCreateNumericArray before the loops, and use mxGetPr to retrieve output pointer. Then fill up the entries.
Titus

More Answers (2)

Jon
Jon on 11 Jan 2012
Hi Titus,
Thank you so much for your help; you just made the next 5 years of my graduate studies a lot easier! I knew it was a memory issue, like you said, but I didn't see what was going on/how to correct it - your response was great. Thank you.
Best,
Jon
P.S. for those who want to see the correction to this problem I have attached the corrected .cpp code (the script code was fine):
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% .cpp code
#include mex.h #include math.h
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { mwSize r,c, i,j,k, n; mwSize Ndims; const mwSize *dims;
dims = mxGetDimensions(prhs[0]);
Ndims = mxGetNumberOfDimensions(prhs[0]);
/* get size of the matrix */
r = mxGetM(prhs[0]);
c = mxGetN(prhs[0]);
double *input;
double *output;
double temp[128][128][27];
/* get pointer to data */
input = mxGetPr(prhs[0]);
/* Create a matrix for the return argument */
plhs[0] = mxCreateNumericArray(Ndims,dims,mxDOUBLE_CLASS,mxREAL);
output = mxGetPr(plhs[0]);
/* access matrix using row/column indices */
mwSize count = 0;
for (i=0; i<128; i++) {
for (j=0; j<128; j++) {
for (k=0; k<27; k++) {
temp[i][j][k] = input[count] * input[count];
count++;
}
}
}
count = 0;
for (i=0; i<128; i++) {
for (j=0; j<128; j++) {
for (k=0; k<27; k++) {
output[count] = temp[i][j][k];
count++;
}
}
}
mxSetPr(plhs[0], output);
return;
}
  1 Comment
Titus Edelhofer
Titus Edelhofer on 11 Jan 2012
Good to hear. You might want to remove the mxSetPr call (it's unnecessary). It doesn't hurt but will confuse the reader ...

Sign in to comment.


Jon
Jon on 11 Jan 2012
Titus,
I did have one more general question for you and would appreciate your input on this.... I was wondering if you were familiar with the differences between having mex files open C code and bringing it back to matlab and using matlab code from C based code. Do you have a preference in which way you code (my main concern is computation time in matlab)?
I ask because I'm involved in biomedical research that uses GUIs to drive image processing. While I know compiled languages are faster, I'm much more comfortable coding GUIs in matlab. My main issue with matlab is that some of my data sets are so large that matlab can take hours or days to run even when optimized by using meshgrid instead of for loops for indexing my code - this is unrealistic.
Best,
Jon

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!