C++ 导致 SIGABRT 故障的原因是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11161126/
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 14:53:59  来源:igfitidea点击:

What causes a SIGABRT fault?

c++

提问by user1444426

Could you please tell me what could possibly cause a SIGABRT fault in C++?

你能告诉我什么可能导致 C++ 中的 SIGABRT 错误吗?

回答by ecatmur

Per Wikipedia,

根据维基百科

SIGABRTis sent by the process to itself when it calls the abortlibc function, defined in stdlib.h. The SIGABRTsignal can be caught, but it cannot be blocked; if the signal handler returns then all open streams are closed and flushed and the program terminates (dumping core if appropriate). This means that the abortcall never returns. Because of this characteristic, it is often used to signal fatal conditions in support libraries, situations where the current operation cannot be completed but the main program can perform cleanup before exiting. It is used when an assertion fails.

SIGABRT由进程在调用abortlibc 函数时发送给自身,定义在stdlib.h. 该SIGABRT信号可以被捕获,但它不能被阻止; 如果信号处理程序返回,则所有打开的流都将关闭并刷新,程序终止(如果合适,则转储核心)。这意味着abort调用永远不会返回。由于这个特性,它通常用于在支持库中发出致命条件信号,即当前操作无法完成但主程序可以在退出前执行清理的情况。它在断言失败时使用。

That means that if your code is not calling abortdirectly nor sending itself the SIGABRTsignal via raise, and you don't have any failing assertions, the cause must be that a support library (which could be libc) has encountered an internal error. If you provide the details of your program we might be able to suggest possible causes. Even better, if you examine a core or run your program in a debugger you should be able to collect a stack trace, which will show which library caused your program to abort.

这意味着如果您的代码既没有abort直接调用也没有通过 向自身发送SIGABRT信号raise,并且您没有任何失败的断言,则原因一定是支持库(可能是 libc)遇到了内部错误。如果您提供程序的详细信息,我们可能会建议可能的原因。更好的是,如果您检查内核或在调试器中运行您的程序,您应该能够收集堆栈跟踪,这将显示哪个库导致您的程序中止。

(It is also possible that another program on your system is sending your program SIGABRT, but this is in most cases vanishingly unlikely.)

(也有可能是您系统上的另一个程序正在发送您的程序SIGABRT,但在大多数情况下,这种情况几乎不可能发生。)

回答by Andrew

This usually happens when libraries encounter internal error, so they call abort(), because they cant continue. This might happen when you overwrite one of it's data structures (the one which belongs to the function from libc for example). So this might be e.g. libc calling because you did overwrite something. And the application must then terminate because it's impossible to continue or handle it, which is called failed assertion.

这通常发生在库遇到内部错误时,因此它们调用 abort(),因为它们无法继续。当您覆盖其中一个数据结构(例如属于 libc 中的函数的数据结构)时,可能会发生这种情况。因此,这可能是例如 libc 调用,因为您确实覆盖了某些内容。然后应用程序必须终止,因为它不可能继续或处理它,这称为失败断言。

回答by Rafael Baptista

In practice this is usually triggered by the assert macro:

在实践中,这通常由 assert 宏触发:

char* foo = NULL;
assert( foo != NULL );

would result in SIGABRT

会导致 SIGABRT