How to find memory leaks in C++?
Memory leaks in C++ are dangerous thing that may lead your application to crash. Imagine your code allocates new blocks of memory again and again. At the some point no more free memory available and the process just crashes.
Fortunately C++ comes with destructors that are automatically called when a variable goes out of scope. This features allows developers to use a good approach called RAII. RAII means that you don’t allocate memory manually each time but rather you use wrappers. Sometimes they are called “smart pointers” because they are smart enough to free resources (e.g. memory) when no one needs them.
RAII is a good method but sometimes it’s impossible to rewrite legacy code. Either a smart wrapper is not smart and contains a mistake: resources are not freed as expected. What to do in this case?
In such cases memory profilers to the rescue. Memory profiler is a tool that allows to view what allocations have been made along with source file references.
Deleaker is a memory profiler for Visual C++ developers. It comes as an extension for Visual Studio:
and as a standalone tool:
Besides memory leaks Deleaker also shows GDI leak, handles leaks, leaks made by COM/OLE and many others. You can filter out what leak types you are interested in. Download it from www.deleaker.com