Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I debug St9bad_alloc failures in gdb in C?

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.

like image 856
Nathan Fellman Avatar asked Nov 02 '09 09:11

Nathan Fellman


People also ask

Which command is used to delete the breakpoint?

With the clear command you can delete breakpoints according to where they are in your program. With the delete command you can delete individual breakpoints, watchpoints, or catchpoints by specifying their breakpoint numbers.

What is GDB in C?

Gdb is a debugger for C (and C++). It allows you to do things like run the program up to a certain point then stop and print out the values of certain variables at that point, or step through the program one line at a time and print out the values of each variable after executing each line.

How do I start GDB on Linux?

Go to your Linux command prompt and type “gdb”. Gdb open prompt lets you know that it is ready for commands. To exit out of gdb, type quit or q.


1 Answers

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.

like image 170
alexkr Avatar answered Oct 12 '22 09:10

alexkr