cppMemDbg – Easy to use C++ memory leak detection library

This library is the C++ sequel to the cMemDbg.

Just as the cMemDbg, it is a very easy to use library which can help you to detect and track memory leaks.

Its usage is very similar to the cMemDbg, but with support for the C++ operators (new, new[], delete and delete[]).

There are lots of solutions for this on the net, but this one has the particularity of being really simple to implement.

>new  003D26D8  36  [Main.cpp:127]
>new  003D2708  36  [Main.cpp:128]
>ERROR  Bad free type  free => delete  003D2708  36
(Main.cpp:128)
>free  003D2708  36  (Main.cpp:128)  [Main.cpp:129]
>free  003D2708  0    [Main.cpp:130]
>ERROR  Trying to free unallocated memory: 003D2708
[Main.cpp:130]
>delete[]  003D3EB0  7  (String.cpp:59)  [String.h:41]
[...]
>delete[]  003D24F0  4  (String.cpp:59)  [String.h:41]
>delete  003D2490  40  (Lista.h:120)  [Lista.h:112]
>INFO  PROBLEM: Memory leak found (36 bytes)
>INFO  Unfreed block  003D26D8  36    [Main.cpp:127]

You just have to add the following include to your main include file (a file that gets included by each file of your project) or, in case you don’t have one, to each file that calls any memory allocation function (malloc, realloc, calloc, free, new, new[], delete or delete[]).

This is the line:

#include "cMemDbg.h"

There are two extra cautions you have to take into account:
1- Include it after the standard headers (stdio.h, stdlib.h, malloc.h, etc.).
2- Never call the delete (or delete[]) operator without knowing if its argument is NULL. So: “delete a;” should become “if (a) delete a;”. Otherwise, the library may show incorrect messages.

That’s all… Expecting more? Well, there is a final step… You have to make this call just before you exit your program so you can get the detailed conclusions:

PrintMemoryLeakInfo();

By default, the library output will go to stdout (normally the console screen). If you want to redirect it to a file, you can call the InitCPPMemDbg() function. This is its prototype:

void InitCPPMemDbg(const char *pszOutputPath = NULL);

Other option, to simplify, is to create a global object of the “cppMemDbg” type at the beginning of the global declarations. You can pass a file path to the constructor to redirect the output. It will call InitCPPMemDbg (to setup the redirect) at construction and PrintMemoryLeakInfo() at destruction.

Simply like this:

cppMemDbg cDbg;

Or, with redirection:

cppMemDbg cDbg("mylocalfile.txt");

Easy, not?

That was all… Really.

The code completely is portable.

GNU GPL v3 cppMemDbg is licensed under the GNU GPL v3 (attached)…

Now, before going to more “expert” features, I’ll leave here the link to download the library for the ones who don’t wish to keep reading:

Now let’s continue…

The library comes with three configurable settings (available in cppMemDbg.cpp):

  • PRINT_OPERATIONS: If set to 1, it will print to the configured output (stdout by default) each memory alloc operation done in the program (allocation or free). Otherwise, the library will just print problems and notifications and the final dump. [Default value = 1]
  • MAX_ALLOC: Internal library memory stack length (in elements). It is the max amount of allocations that can be tracked without being freed. You can increase this value at will if needed. In fact, if it is needed, the library will print a message saying: “INTERNAL_ERROR: Allocation stack overflow, please increase MAX_ALLOC”. [Default value = 256]
  • g_fFile: FILE* to print the library generated notifications to. [Default value = stdout]
  • MAX_DELETE_STACK: Internal library delete nesting stack array length (in elements). It is the max amount of deletes that can be nested. You can increase this value at will if needed. In fact, if it is needed, the library will print a message saying: “INTERNAL_ERROR: Delete stack overflow, please increase MAX_DELETE_STACK”. [Default value = 16]

Finally, there are two more functions you could use wherever needed:

  • PrintTotalAllocatedMemory(): Prints the accumulative amount of memory allocated at the moment of the call.
  • PrintMemoryReservedByCMemDbgLibrary(): Prints the amount of memory reserved by the library (defined at compilation time by the MAX_ALLOC constant).

That’s all… I said it was easy to use.

Anyway, if after reading the cppMemDbg.h file’s comments, you have any questions, comments or suggestions, please feel free to contact me.

Here is the Attendance Control project modified to test it for memory leaks and allocation/deallocation problems with the cppMemDbg library:

The code is portable between Linux and Windows (I’ve tested it myself on both platforms and it worked seamlessly).

It’s been developed, compiled and tested using wxDev-C++ for Windows with the MinGW compiler (included in the bundle). In Linux, it was compiled using the GNU GCC compiler.

It is configured to send the output to the “TestMemDbg.txt” file in the working directory. This is the relevant piece of the Main.cpp file:

//...

#include "ControlDeAsistencia.h"
#include "BasicFunctions.h"
#include <fstream>

#include "cppMemDbg.h"

cppMemDbg cDbg("TestMemDbg.txt");

//...

And this is its successful output (the real output es tabbed to make it easier the analysis en a spreadsheet software):

>new[]  003D3F58  14  [String.cpp:59]
>new[]  003D2438  14  [String.cpp:59]
>delete[]  003D3F58  14  (String.cpp:59)  [String.h:41]
>new[]  003D3EB0  7  [String.cpp:59]
>new[]  003D2450  7  [String.cpp:59]
[...]
>delete[]  003D2558  4  (String.cpp:59)  [String.h:41]
>delete[]  003D24F0  4  (String.cpp:59)  [String.h:41]
>delete  003D2490  40  (Lista.h:120)  [Lista.h:112]
>INFO  No memory leaks detected

Support appreciated!

All the content offered in this website is, except noted otherwise, of free nature. This means you can share it wherever you want if you do it freely and stating its source.

If it was useful for you and you’d like to contribute, you can make a donation or, at least, visit one of our advertisers of your choice; they are all around the site.

Incoming search terms for the article:



1 Responses to “cppMemDbg – Easy to use C++ memory leak detection library”


  • hi,

    i was just trying out your mem leak detector. but when i was trying, i run into the problem the memory detector becomes not notified if the delete operator has been called to an object.

    i tried to debug the problem and the behaviour is that the overloaded new operator becomes called correctly, but the overloaded delete/delete[] becomes not called and therefore it will not do any cleanup of internal vlaues.

    did you ever seen that behaviour??

    I’m working at with:
    Windows 7
    Eclipse (Indigo)
    MinGW 4.4

    Thanks
    Michael

Leave a Reply