问题描述
在 Linux 环境中,当收到glibc detected *** free(): invalid pointer"错误时,我如何识别是哪一行代码导致的?
In Linux environment, when getting "glibc detected *** free(): invalid pointer" errors, how do I identify which line of code is causing it?
有没有办法强制中止?我记得有一个 ENV 变量来控制这个?
Is there a way to force an abort? I recall there being an ENV var to control this?
如何在 gdb 中为 glibc 错误设置断点?
How to set a breakpoint in gdb for the glibc error?
推荐答案
我相信如果你将 env MALLOC_CHECK_
设置为 2,glibc 会在检测到free(): 无效指针"错误.请注意环境变量名称中的尾随下划线.
I believe if you setenv MALLOC_CHECK_
to 2, glibc will call abort()
when it detects the "free(): invalid pointer" error. Note the trailing underscore in the name of the environment variable.
如果 MALLOC_CHECK_
是 1 glibc 将打印free(): invalid pointer"(以及其他错误的类似 printfs).如果 MALLOC_CHECK_
为 0,glibc 将默默地忽略此类错误并简单地返回.如果 MALLOC_CHECK_
是 3 glibc 将打印消息然后调用 abort()
.IE.它是一个位掩码.
If MALLOC_CHECK_
is 1 glibc will print "free(): invalid pointer" (and similar printfs for other errors). If MALLOC_CHECK_
is 0, glibc will silently ignore such errors and simply return. If MALLOC_CHECK_
is 3 glibc will print the message and then call abort()
. I.e. its a bitmask.
您也可以使用 0-3 的参数调用 mallopt(M_CHECK_ACTION, arg)
,并获得与 MALLOC_CHECK_
相同的结果.
You can also call mallopt(M_CHECK_ACTION, arg)
with an argument of 0-3, and get the same result as with MALLOC_CHECK_
.
既然您看到了free(): invalid pointer"消息,我想您一定已经在设置 MALLOC_CHECK_
或调用 mallopt()
.默认情况下,glibc 不打印这些消息.
Since you're seeing the "free(): invalid pointer" message I think you must already be setting MALLOC_CHECK_
or calling mallopt()
. By default glibc does not print those messages.
至于如何调试它,为 SIGABRT
安装一个处理程序可能是最好的方法.您可以在处理程序中设置断点或故意触发核心转储.
As for how to debug it, installing a handler for SIGABRT
is probably the best way to proceed. You can set a breakpoint in your handler or deliberately trigger a core dump.
这篇关于如何在“检测到 glibc *** free(): 无效指针"上强制中止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!