如何在 C 语言中调试 gdb 中的 St9bad_alloc 故障?

发布于 2024-08-09 10:30:18 字数 278 浏览 8 评论 0原文

我有一个程序失败:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

翻了热茶 2024-08-16 10:30:18

它并不是真正的 malloc/free 导致异常,它是“new”,它绝对是应用程序的 C++ 部分。看起来您提供的参数对于“新”来说太大而无法分配。

'std::bad_alloc' 是由以下代码引起的,例如:

 int * p = new int[50000000];

What does backtrace say when you load crash dump into gdb?
如果无法生成转储,可以要求 GDB 停止 当抛出或捕获异常时
不幸的是,某些版本的 GDB 仅支持以下语法:

catch throw

它允许您在抛出任何异常时中断应用程序。
但是,在帮助中您会看到它应该可以

catch throw std::bad_alloc

在较新的版本中运行。

并且不要忘记:

(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:

 int * p = new int[50000000];

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:

catch throw

which allows you to break application when any exception is thrown.
However, in help you see that it should be possible to run

catch throw std::bad_alloc

in newer versions.

And don't forget that:

(gdb) help catch

is a good source for other useful information.

妄断弥空 2024-08-16 10:30:18

发生这种情况的原因很可能是某些内存被覆盖,从而破坏了内存分配系统的状态(通常在内存块返回到应用程序之前或之后保留)。

如果您有可能(即您使用的是 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.

肥爪爪 2024-08-16 10:30:18

当我尝试读取不存在的文件时,我遇到了这个问题...我尝试为文件内容创建一个内部缓冲区,但因为该文件不存在,所以我创建的缓冲区搞砸了。

int lenth_bytes;
length_bytes = in_file.tellg();
new char [length_bytes]; // length_bytes hadn't been initialised!!!!

记住孩子们,总是初始化你的变量: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.

int lenth_bytes;
length_bytes = in_file.tellg();
new char [length_bytes]; // length_bytes hadn't been initialised!!!!

Remember kids, always initialise your variables :D and check for zero cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文