C++ 运行时计数器退出代码 139,但 gdb 通过

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15600296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:31:44  来源:igfitidea点击:

Counter exit code 139 when running, but gdb make it through

c++gdbsegmentation-faultundefined-behavior

提问by ulyssis2

My question sounds specific, but I doubt it still can be of a C++ debug issue.

我的问题听起来很具体,但我怀疑它仍然是 C++ 调试问题。

I am using omnet++ which is to simulate wireless network. omnet++ itself is a c++ program.

我正在使用 omnet++ 来模拟无线网络。omn​​et++ 本身是一个 C++ 程序。

I encountered a queer phenomena when I run my program (modified inet framework with omnet++ 4.2.2 in Ubuntu 12.04): the program exit with exit code 139 (people say this means memory fragmentation) when touching a certain part of the codes, when I try to debug, gdb doesn't report anything wrong with the 'problematic' codes where the simulation exits previously, actually, the debug goes through this part of codes and output expected results.

我在运行我的程序时遇到了一个奇怪的现象(在 Ubuntu 12.04 中使用 omnet++ 4.2.2 修改了 inet 框架):当我触摸代码的某个部分时,程序以退出代码 139 退出(人们说这意味着内存碎片)尝试调试,gdb 不会报告模拟之前退出的“有问题”代码有任何问题,实际上,调试会通过这部分代码并输出预期结果。

gdb version info: GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04

gdb 版本信息:GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04

Could anybody tell me why the run fails but debug doesn't?

谁能告诉我为什么运行失败但调试没有?

Many thanks!

非常感谢!

回答by Employed Russian

exit code 139 (people say this means memory fragmentation)

退出代码 139(人们说这意味着内存碎片)

No, it means that your program died with signal 11(SIGSEGVon Linux and most other UNIXes), also known as segmentation fault.

不,这意味着您的程序因信号而终止11SIGSEGV在 Linux 和大多数其他 UNIX 上),也称为segmentation fault.

Could anybody tell me why the run fails but debug doesn't?

谁能告诉我为什么运行失败但调试没有?

Your program exhibits undefined behavior, and can do anything(that includes appearing to work correctly sometimes).

您的程序表现出未定义的行为,并且可以做任何事情(包括有时看起来工作正常)。

Your firststep should be running this program under Valgrind, and fixing all errors it reports.

你的第一个步骤应该Valgrind的下运行此程序,并修复它报告的所有错误。

If after doing the above, the program still crashes, then you should let it dump core (ulimit -c unlimited; ./a.out) and then analyze that core dump with GDB: gdb ./a.out core; then use wherecommand.

如果执行上述操作后,程序仍然崩溃,那么您应该让它转储 core ( ulimit -c unlimited; ./a.out),然后使用 GDB 分析该 core dump: gdb ./a.out core; 然后使用where命令。

回答by HamzaMushtaq

this error is also caused by null pointer reference. if you are using a pointer who is not initialized then it causes this error.

此错误也是由空指针引用引起的。如果您使用的是未初始化的指针,则会导致此错误。

to check either a pointer is initialized or not you can try something like

要检查指针是否已初始化,您可以尝试类似的操作

Class *pointer = new Class();
if(pointer!=nullptr){
    pointer->myFunction();
}