MEX 文件 运行 时检测到错误,导致 matlab 崩溃?

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

我写了一个 mex 函数(在 C 中),它从 matlab 中获取 n*4,然后将它们转换为结构,最后将 k*5 输出到 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;
    }
}

它被 mex 函数正确编译,但是当我 运行 代码时,matlab 崩溃了,我收到了这个错误:

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. 

我将 14 个输入参数称为

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);

而第14个是矩阵n*4单矩阵。 任何建议都可以帮助我知道错误在哪里?

我可以将你的代码减少到这些行,但仍然会导致崩溃(我没有编译任何这些,所以我在这里解释一下):

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;
}
}

这是因为你创建了一个 std::vector<Box> 但没有给它一个大小,然后赋值给它。

改为这样做:

Boxes boxes(k);