Debugging strategy to find the cause of bad_alloc

前端 未结 6 737
Happy的楠姐
Happy的楠姐 2021-01-30 13:04

I have a fairly serious bug in my program - occasional calls to new() throw a bad_alloc.

From the documentation I can find on bad_alloc, it seems to be thrown for these

6条回答
  •  执笔经年
    2021-01-30 13:42

    A strategy for debugging this would be to set a catchpoint in the debugger. In gdb one would say “catch throw” to ask the debugger you stop whenever the program throws an exception. You can refine that to catch only std::bad_alloc if the program throws and catches lots of exceptions in its normal course of business. The debugger will stop inside whichever allocation function generates std::bad_alloc, allowing you to poke around. In this case, you would quickly learn that number of bytes requested is not what you expect.

提交回复
热议问题