MEX 文件中的断言导致 Matlab 崩溃

发布于 2024-12-21 03:50:42 字数 847 浏览 4 评论 0原文

我在我的 C++ 代码中使用由 ma​​trix.h 定义的 mxAssert 宏,mex 可以完美编译。当我调用的 mex 代码中违反断言时,该断言不会导致我的程序崩溃,而是导致 Matlab 本身崩溃。我错过了什么吗?这是有意的行为吗? 当我查看 Matlab 的崩溃报告时,引起的断言与我的代码引发的断言完全相同 - 包括我的描述性描述...我是否必须以某种方式运行我的 mex 代码,以便 Matlab 可以识别 mex 代码引起的断言(类似尝试捕捉)? 可能还有另一种方法可以安全地停止我的 mex 代码并返回到 Matlab 提示符。

预先感谢您,非常感谢您的帮助!

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

编译的编辑:一个让我的 matlab 屈服的最小示例:

#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");
}
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木緿 2024-12-28 03:50:42

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

我想这是有道理的,因为这就是断言应该做的事情。

我强烈建议您使用类似的方法:

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

否则,我的一个朋友有以下技巧(使用预处理器指令):

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

打印单个错误字符串,例如 myassert(A=B,"A not B") ,你可以稍微增强一下:

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

他还告诉我,你可以使用类似的东西来改进它:

#isOK,__LINE__,__PRETTY_FUNCTION__, __FILE__

...为了打印行号等等。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文