1

When I want to new an object in C++, I need to consider what should I do when there is not enough memory, so I wrote the following code:

CacheHeapItem* m_Items;
try{
    m_Items = new CacheHeapItem[m_Count];
}catch(const bad_alloc& e){
    DebugBreak();
}

But I am not sure if the DebugBreak function will be executed when there is insufficient memory?

piet.t
  • 11,718
  • 21
  • 43
  • 52
alice.Li
  • 51
  • 4
  • 2
    If you're really paranoid, you can allocate some memory on startup and free it right before calling `DebugBreak`. – David Schwartz Apr 18 '17 at 07:58
  • Visual Studio will break on exception by default (and if not, configure it like so), so you don't really need to call `DebugBreak()` – Ivan Aksamentov - Drop Apr 18 '17 at 08:30
  • these codes are running at a mul-thread env. If I free memory at this point, I'm not quite sure I can allocate the memory again.. do you have code example to get an "atomic free-allocate" action... – alice.Li Apr 18 '17 at 08:30
  • you should reconsider the whole deal. in release mode if no debugger is available your app will probably just crash. better protect from out of memory errors otherwise – David Haim Apr 18 '17 at 08:57
  • or just *let it crash*... most of the time it doesn't worth the effort to handle these crazy cases (of course I'm saying this in general, your use case might be different). – Karoly Horvath Apr 18 '17 at 09:29

2 Answers2

2

If you use microsoft compiler use __debugbreak() which is functionally identical to DebugBreak() winapi function. It's unlikely that it allocates any memory, as it simply inserts __asm 3 opcode (for x86 and equivalent opcode on arm).

This is obviously not the best code for release, you may check for debugger presence and break only if IsDebuggerPresent:

CacheHeapItem* m_Items;
try{
    m_Items = new CacheHeapItem[m_Count];
}catch(const bad_alloc& e){
    if (IsDebuggerPresent())
        __debugbreak();
}
Pavel P
  • 15,789
  • 11
  • 79
  • 128
  • it doesn't answer the question. `call 0x12345` also adds just one assembly instruction but what does `0x12345` actually do? it may call `malloc`. It could be that attaching a debugger might cause some debug-related data structures be dynamically allcoated. – David Haim Apr 18 '17 at 08:36
  • `__asm 3` or more clearly `INT 3` doesn't do anything in the program. In fact, it's one of the most absolute ways to stop a program from doing anything. The program stops right there at that very instruction, as the CPU just halts the thread outright and tells the OS to deal with the situation. Windows will then suspend the other program threads and invoke a debugger. That debugger is _another process_ with its own address space. – MSalters Apr 19 '17 at 12:33
0

If you want to catch some exceptions you should configure exception filters. This way you won't need to write any special debug handling code that must be removed in release build (DebugBreak will cause process to be terminated if no debugger is present).

user7860670
  • 35,849
  • 4
  • 58
  • 84