如何在 C 语言中调试 gdb 中的 St9bad_alloc 故障?
我有一个程序失败:
terminate called after throwing an instance of 'std::bad_alloc'
what(): St9bad_alloc
我想这与 malloc
/free
有关,但我不知道是哪一个。
我可以在 gdb 中设置什么断点来在错误发生时中断,以便我可以查看堆栈跟踪?
该程序是C和C++的组合,使用gcc 3.4.2编译。
I have a program failing with:
terminate called after throwing an instance of 'std::bad_alloc'
what(): St9bad_alloc
I imagine it's something to do with malloc
/free
, but I don't know which one.
What breakpoint can I in gdb set that will break on the error so that I can view a stack trace?
The program is a combination of C and C++, compiled with gcc 3.4.2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它并不是真正的 malloc/free 导致异常,它是“new”,它绝对是应用程序的 C++ 部分。看起来您提供的参数对于“新”来说太大而无法分配。
'std::bad_alloc' 是由以下代码引起的,例如:
What does backtrace say when you load crash dump into gdb?
如果无法生成转储,可以要求 GDB 停止 当抛出或捕获异常时。
不幸的是,某些版本的 GDB 仅支持以下语法:
它允许您在抛出任何异常时中断应用程序。
但是,在帮助中您会看到它应该可以
在较新的版本中运行。
并且不要忘记:
是其他有用信息的良好来源。
It is not really malloc/free which causes the exception, it is "new" which is definitely in C++ part of your application. It looks like you are providing a parameter which is too big for "new" to allocate.
'std::bad_alloc' is caused by the following code for example:
What does backtrace says when you load crash dump into gdb?
If you cannot generate dump, you can ask GDB to stop when exception is thrown or caught.
Unfortunately, some versions of GDB support only the following syntax:
which allows you to break application when any exception is thrown.
However, in help you see that it should be possible to run
in newer versions.
And don't forget that:
is a good source for other useful information.
发生这种情况的原因很可能是某些内存被覆盖,从而破坏了内存分配系统的状态(通常在内存块返回到应用程序之前或之后保留)。
如果您有可能(即您使用的是 x86 Linux),请在 Valgrind 中运行您的程序,它通常可以向您准确显示腐败发生的位置。
It's quite possible that this happens due to some memory being overwritten, thus corrupting the memory allocation system's state (which is generally kept either before or after the memory blocks returned to the application).
If you have the possibility (i.e., you're on x86 Linux), run your program in Valgrind, it can often show you exactly where the corruption happens.
当我尝试读取不存在的文件时,我遇到了这个问题...我尝试为文件内容创建一个内部缓冲区,但因为该文件不存在,所以我创建的缓冲区搞砸了。
记住孩子们,总是初始化你的变量:D并检查零情况。
I have had this when trying to read in a file that doesn't exist... I'd try to create an internal buffer for the file contents, but because the file didn't exist, my creation of the buffer screwed up.
Remember kids, always initialise your variables :D and check for zero cases.