Search code examples
cmatlabcrashmex

Matlab crashes while running a mex function with some data


I have written a mex function(in C) which takes 2 arrays and a scalar as input and after doing some mathematical calculation it returns a scalar quantity as output. I can compile the corresponding mex function successfully on the MATLAB platform but as soon as I am gonna run it with some input data, its causing a crash to MATLAB. The error log has a heading "Segmentation Violation detected at Mon Apr 25 ..:..:.. 2016". I also tried to debug it in Linux platform using the GNU debugger 'gdb'. It is showing a problem with all the if statements that I have used to verify the number and type of input/output arguments using nrhs,prhs[],nlhs,plhs[]. For example my first statement for checking the number of input arguments is,

if(nrhs!=3) 
   mexErrMsgTxt("Error..Three inputs required.");

and likewise others for nlhs. The GNU debugger is placing its first breakpoint at the above if statement and if I am commenting it out it is causing a problem with the second if statement following this and likewise. When I am commenting out all the if statements the mex function is running successfully and also giving me the desired output.

It's been long since I am trying to remove this bug by reading all the available answers but I am not unable to do so. Please help me with the above problem. Thanks in advance.

Below is the actual code:

void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]) 
{
    double *Ip, *Is;            /* Input data vectors */
    double r;               /* Value of r (input) */
    double *dist;           /* Output ImED distance */
    size_t ncols;           /* For storing the size of input vector */

    /* Checking for proper number of arguments */
    if(nrhs!=3) 
        mexErrMsgTxt("Error..Three inputs required.");

    if(nlhs!=1) 
        mexErrMsgTxt("Error..Only one output required.");

    /* make sure the first input argument(value of r) is scalar */
    if( !mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) || mxGetNumberOfElements(prhs[0])!=1 ) 
        mexErrMsgTxt("Error..Value of r must be a scalar.");

    /* make sure that the input vectors are of type double */
    if(!mxIsDouble(prhs[1]) || mxIsComplex(prhs[1]) || !mxIsDouble(prhs[2]) || mxIsComplex(prhs[2]))          
        mexErrMsgTxt("Error..Input vectors must be of type double.");       

    /* Make sure that the output is of type double and is a scalar */
    if(!mxIsDouble(plhs[0]) || mxIsComplex(plhs[0]) || mxGetNumberOfElements(plhs[0])!=1) 
        mexErrMsgTxt("Error..Image Euclidean Distance must be a scalar.");

    /* check that number of rows in input arguments is 1 */
    if(mxGetM(prhs[1])!=1 || mxGetM(prhs[2])!=1) 
        mexErrMsgTxt("Error..Inputs must be row vectors."); 

    /* Get the value of r */
    r = mxGetScalar(prhs[0]);

    /* Getting the input vectors */
    Ip = mxGetPr(prhs[1]);
    Is = mxGetPr(prhs[2]);

    ncols = mxGetN(prhs[1]);

    /* Creating link for the scalar output */
    plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
    dist = mxGetPr(plhs[0]); 

    imedDistCal(r,Ip,Is,(mwSize)ncols,dist);
}

Solution

  • As mentioned in the comment above, MATLAB crashes because the mxWhateverFunction(plhs[0]) <-- fill in for whatever results in an invalid address when plhs[0] isn't linked to any variable prior to the tests.

    The following piece of code

     if(!mxIsDouble(plhs[0]) || mxIsComplex(plhs[0]) || mxGetNumberOfElements(plhs[0])!=1) 
            mexErrMsgTxt("Error..Image Euclidean Distance must be a scalar.");
    

    should be moved to after the following to avoid this problem.

       /* Creating link for the scalar output */
        plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
        dist = mxGetPr(plhs[0]);