断言在MEX文件导致Matlab崩溃 [英] Assertion in MEX file causes Matlab to crash

查看:392
本文介绍了断言在MEX文件导致Matlab崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用我的C ++代码中的 matrix.h 定义的 mxAssert -macro,mex完全编译。当在我的调用mex代码中违反断言时,这个断言不会导致我的程序崩溃,但是Matlab本身。我缺少什么东西?这是预期的行为吗?
当我看看Matlab的崩溃报告,原因断言是由我的代码提出的 - 包括我的描述性描述...我必须以某种方式运行我的mex代码,以便Matlab可以识别mex代码引起断言(类似于try-catch)?
可能有另一种方法安全地停止我的mex代码并返回到Matlab提示符。



提前感谢,任何帮助非常感谢! >

编辑:使用命令编译代码 mex -v Temp.cpp -g



编辑:一个最小的例子,使我的matlab膝盖:

  #include< matrix.h> 
class Temp {
public:
Temp();
virtual〜Temp();
};

Temp :: Temp(){
// TODO自动生成的构造函数stub
}

Temp ::〜Temp(){
// TODO自动生成的析构函数stub
}

externC{
void mexFunction(int nlhs,mxArray * plhs [],int nrhs,const mxArray * prhs []){
int foo = 10;
mxAssert(foo == 11,foo is not 10);
}
}


解决方案

我的系统(Ubuntu 64),它也崩溃了。



我想这是感觉,因为这是assert应该做的。



我强烈建议你使用类似:

  if(error){mexErrMsgTxt \\ n);} 

否则,我的一个朋友有:

  #define assert(isOK)((isOK)?(void)0:(void)mexErrMsgTxt \\ n))

打印单个错误字符串,例如 myassert(A = B,A not B),您可以加强这一点:

  #define myassert(isOK,astr)(isOK)?(void)0:(void)mexErrMsgTxt(astr))
  #isOK,__ LINE __,__ PRETTY_FUNCTION__,__FILE__ 

...为了打印行号等。


I'm using the mxAssert-macro defined by matrix.h in my C++ code which mex perfectly compiles. When an assertion is violated in my called mex code, this assertion causes not my program to crash but Matlab itself. Am I missing something out? Is that intended behavior? When I look at Matlab's crash report, the causing assertion is the very same raised by my code - including my descriptive description... Do I have to run my mex code in a certain way so that Matlab can recognize mex code caused assertions (similar to try-catch)? Probably there's another way to safely stop my mex code and return to the Matlab prompt.

Thank you in advance, any help is very appreciated!

EDIT: the code is compiled with the command mex -v Temp.cpp -g

EDIT: a minimal example that brings my matlab to its knees:

#include <matrix.h>
class Temp {
public:
    Temp();
    virtual ~Temp();
};

Temp::Temp() {
    // TODO Auto-generated constructor stub
}

Temp::~Temp() {
    // TODO Auto-generated destructor stub
}

extern "C" {
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    int foo = 10;
    mxAssert(foo==11, "foo is not 10");
}
}

解决方案

On my system (Ubuntu 64), it crashes too.

I guess it makes senss, because that's what assert is supposed to do.

I strongly advise you to use something like:

if(error){mexErrMsgTxt("assert failed\n");}

Otherwise, one of my friends have the following trick (with preprocessor instructions):

#define assert( isOK )       ( (isOK) ? (void)0 : (void) mexErrMsgTxt("assert failed\n") )

To print individual error strings, e.g. myassert(A=B,"A not B") , you can enhance this a little bit:

#define myassert( isOK,astr )      ( (isOK) ? (void)0 : (void) mexErrMsgTxt(astr) ) 

He also told me that you can improvie it using something like:

#isOK,__LINE__,__PRETTY_FUNCTION__, __FILE__

...in order to print the line number and so on.

这篇关于断言在MEX文件导致Matlab崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆