Search code examples
matlabcrashmex

error was detected while a MEX-file was running which crashed matlab?


I have written a mex function(in C) which takes n*4 from matlab , then convert them in struct, finally output the k*5 to the matlab.

typedef vector<float> vectorf;
typedef vector<int> vectori;
typedef Array<float> arrayf;
typedef Array<int> arrayi;
typedef struct { int c, r, w, h; float s; } Box;
typedef vector<Box> Boxes;
void mexFunction( int nl, mxArray *pl[], int nr, const mxArray *pr[] )
{
    if(mxGetClassID(pr[13])!=mxSINGLE_CLASS) mexErrMsgTxt("boxes must be a float*");
if((int) mxGetN(pr[13])!=4) mexErrMsgTxt("boxes must have 4 columns");
float * box= (float *)mxGetData(pr[13]); //input to mex function
int k= (int) mxGetM(pr[13]) ; Boxes boxes;
for(int i=0; i<k; i++){
   boxes[i].c= box[i + 0*k]-1;
   boxes[i].r= box[i + 1*k]-1;
   boxes[i].w= box[i + 2*k];
   boxes[i].h= box[i + 3*k];
   boxes[i].s=0;
}
// optionally create memory for visualization
arrayf V; if( nl>1 ) {
  const mwSize ds[3] = {h,w,3};
  pl[1] = mxCreateNumericArray(3,ds,mxSINGLE_CLASS,mxREAL);
  V._x = (float*) mxGetData(pl[1]); V._h=h; V._w=w;
}
int n = (int) boxes.size();  //create output to matlab
pl[0] = mxCreateNumericMatrix(n,5,mxSINGLE_CLASS,mxREAL);
float *bbs = (float*) mxGetData(pl[0]);
for(int i=0; i<n; i++) {
      bbs[i + 0*n ] = (float) boxes[i].c+1;
      bbs[ i + 1*n ] = (float) boxes[i].r+1;
      bbs[ i + 2*n ] = (float) boxes[i].w;
      bbs[ i + 3*n ] = (float) boxes[i].h;
      bbs[ i + 4*n ] = boxes[i].s;
    }
}

its compiled correctly by mex function but when i run the code the matlab is crashed and i received this error:

Configuration:
  Crash Decoding           : Disabled - No sandbox or build area path
  Crash Mode               : continue (default)
  Default Encoding         : windows-1252
  Deployed                 : false
  Graphics Driver          : Unknown hardware 
  Graphics card 1          : Intel Corporation ( 0x8086 ) Intel(R) HD Graphics Family Version 20.19.15.4835 (2017-10-16)
  Java Version             : Java 1.8.0_144-b01 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
  MATLAB Architecture      : win64
  MATLAB Entitlement ID    : 6257193
  MATLAB Root              : C:\Program Files (x86)
  MATLAB Version           : 9.4.0.813654 (R2018a)
  OpenGL                   : hardware
  Operating System         : Microsoft Windows 10 Pro
  Process ID               : 5904
  Processor ID             : x86 Family 6 Model 69 Stepping 1, GenuineIntel
  Session Key              : e3a6c784-70a3-4032-9402-bbff2b8d5ce9
  Window System            : Version 10.0 (Build 16299)
    This error was detected while a MEX-file was running. If the MEX-file
    is not an official MathWorks function, please examine its source code
    for errors. Please consult the External Interfaces Guide for information
    on debugging MEX-files. 

I call 14 input arguments as

bbs=edgeBoxesMex(E,O,o.alpha,o.beta,o.minScore,o.maxBoxes,...
  o.edgeMinMag,o.edgeMergeThr,o.clusterMinMag,...
  o.maxAspectRatio,o.minBoxArea,o.gamma,o.kappa,boxes);

and the 14th is a matrix n*4 single matrix. Any suggestion can help me to know where is the error?


Solution

  • I can reduce your code to these lines and still cause the crash (I haven't compiled any of this, so I'm paraphrasing here):

    typedef struct { int c, r, w, h; float s; } Box;
    typedef vector<Box> Boxes;
    void mexFunction( int nl, mxArray *pl[], int nr, const mxArray *pr[] )
    {
    int k=1;
    Boxes boxes;
    for(int i=0; i<k; i++) {
       boxes[i].s=0;
    }
    }
    

    This is because you create a std::vector<Box> but don't give it a size, then assign into it.

    Do this instead:

    Boxes boxes(k);