Probably everyone ran into this problem at least once during development:
while(/*some condition here that somehow never will be false*/)
{
...
yourvector.push_back(new SomeType());
...
}
As you see the program starts to drain all system memory, your program hangs and your system starts to swap like crazy. If you don’t recognize the problem fast enough and kill the process you probably get an unresponsive system in seconds where your mouse pointer don’t even moving. You can either wait your program crash with “out of memory” error (which may take several long minutes) or hit the reset on your computer.
If you can’t track down the bug immediately then you will need several tests and resets to find out which is very annoying…
I’m looking for a possibly cross-platform way to prevent this somehow. The best would be a debug mode code that exits the program if it allocated too much memory, but how can I keep track how much memory is allocated?
Overriding the global new and delete operators won’t help, because the free function I would invoke in the delete won’t give any idea how many bytes are freed.
Any ideas appreciated.
But you can make it so. Here’s a full framework for overloading the global memory operators (throw it in some
global_memory.cppfile):Then you can do something like: