MEX 文件中的断言导致 Matlab 崩溃
我在我的 C++ 代码中使用由 matrix.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我的系统(Ubuntu 64)上,它也崩溃了。
我想这是有道理的,因为这就是断言应该做的事情。
我强烈建议您使用类似的方法:
否则,我的一个朋友有以下技巧(使用预处理器指令):
打印单个错误字符串,例如
myassert(A=B,"A not B")
,你可以稍微增强一下:他还告诉我,你可以使用类似的东西来改进它:
...为了打印行号等等。
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:
Otherwise, one of my friends have the following trick (with preprocessor instructions):
To print individual error strings, e.g.
myassert(A=B,"A not B")
, you can enhance this a little bit:He also told me that you can improvie it using something like:
...in order to print the line number and so on.