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