运行 带有一些数据的 mex 函数时 Matlab 崩溃
Matlab crashes while running a mex function with some data
我写了一个 mex 函数(在 C 中),它接受 2 个数组和一个标量作为输入,并在进行一些数学计算后 returns 一个标量作为输出。我可以在 MATLAB 平台上成功编译相应的 mex 函数,但是一旦我用一些输入数据 运行 它,它就会导致 MATLAB 崩溃。错误日志的标题为 "Segmentation Violation detected at Mon Apr 25 ..:..:.. 2016"。我还尝试使用 GNU 调试器 'gdb' 在 Linux 平台上调试它。它显示了我用来使用 nrhs、prhs[]、nlhs、plhs[] 验证 input/output 参数的数量和类型的所有 if 语句的问题。例如,我检查输入参数数量的第一个语句是,
if(nrhs!=3)
mexErrMsgTxt("Error..Three inputs required.");
以及 nlhs 的其他人。 GNU 调试器将其第一个断点放在上面的 if 语句中,如果我将其注释掉,它会导致第二个 if 语句出现问题,同样如此。当我注释掉所有 if 语句时,mex 函数 运行ning 成功并且还给了我想要的输出。
我已经很久没有尝试通过阅读所有可用的答案来消除此错误,但我并非无法这样做。请帮助我解决上述问题。
提前致谢。
下面是实际代码:
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);
}
如上面的评论所述,MATLAB 崩溃是因为当 plhs[0]
在测试之前未链接到任何变量时,mxWhateverFunction(plhs[0]) <-- fill in for whatever
导致无效地址。
下面这段代码
if(!mxIsDouble(plhs[0]) || mxIsComplex(plhs[0]) || mxGetNumberOfElements(plhs[0])!=1)
mexErrMsgTxt("Error..Image Euclidean Distance must be a scalar.");
应该移到以下后面以避免这个问题。
/* Creating link for the scalar output */
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
dist = mxGetPr(plhs[0]);
我写了一个 mex 函数(在 C 中),它接受 2 个数组和一个标量作为输入,并在进行一些数学计算后 returns 一个标量作为输出。我可以在 MATLAB 平台上成功编译相应的 mex 函数,但是一旦我用一些输入数据 运行 它,它就会导致 MATLAB 崩溃。错误日志的标题为 "Segmentation Violation detected at Mon Apr 25 ..:..:.. 2016"。我还尝试使用 GNU 调试器 'gdb' 在 Linux 平台上调试它。它显示了我用来使用 nrhs、prhs[]、nlhs、plhs[] 验证 input/output 参数的数量和类型的所有 if 语句的问题。例如,我检查输入参数数量的第一个语句是,
if(nrhs!=3)
mexErrMsgTxt("Error..Three inputs required.");
以及 nlhs 的其他人。 GNU 调试器将其第一个断点放在上面的 if 语句中,如果我将其注释掉,它会导致第二个 if 语句出现问题,同样如此。当我注释掉所有 if 语句时,mex 函数 运行ning 成功并且还给了我想要的输出。
我已经很久没有尝试通过阅读所有可用的答案来消除此错误,但我并非无法这样做。请帮助我解决上述问题。 提前致谢。
下面是实际代码:
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);
}
如上面的评论所述,MATLAB 崩溃是因为当 plhs[0]
在测试之前未链接到任何变量时,mxWhateverFunction(plhs[0]) <-- fill in for whatever
导致无效地址。
下面这段代码
if(!mxIsDouble(plhs[0]) || mxIsComplex(plhs[0]) || mxGetNumberOfElements(plhs[0])!=1)
mexErrMsgTxt("Error..Image Euclidean Distance must be a scalar.");
应该移到以下后面以避免这个问题。
/* Creating link for the scalar output */
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
dist = mxGetPr(plhs[0]);