Click here to Skip to main content
11,929,939 members (51,872 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked

Memory leaks dumper with user defined dump information

, 11 Apr 2007
Rate this:
Please Sign up or sign in to vote.
How to add user defined dump information for memory leaks

Screenshot - LeaksDumperScreen.gif


Information for memory leaks provided by the CRT debug library is not very useful. There is only a raw memory snapshot shown. It would be better if developers could see the objects dump in more convenient form, e.g. class variables. Here is the solution. It's only applied to C++ classes.


Let's take a look at the memory leaks dump information provided by the standard CRT debug library:

Detected memory leaks!
Dumping objects ->
e:\net projects\leaksdumperdemo\leaksdumperdemo\common.h(19) : 
    {102} normal block at 0x00359F30, 4 bytes long. Data: <  A > EC 9A 41 00 
Object dump complete.

Here we can see that the dump is represented as series of bytes and it is not clear what is the current state of the object. It could be very useful for developers to see not raw data but the exact values of the object(class) variables. How it could be done? Very simple. The CRT debug library allows you to specify user defined functions which will dump client blocks. The function prototype is:

void DumpClientFunction(void *pUserData, size_t blockSize);


  • pUserData - pointer to the user block
  • blockSize - size of the user block.

To install a user-defined function to dump _CLIENT_BLOCK type memory blocks(debug version only) we need to call


somewhere in our code. But how does one add dump capabilities to the user defined types? The idea is to derive your class from a base class with one virtual function dump_object_info():

class MemoryLeaksDumpBase
    virtual void dump_object_info()=0;

And then override this function and dump all you need there. You may ask why it is a pure virtual, just to let compiler to warn you if you forget to override it in the derived class.

Using the code

Now let's take a look how to use the code in your applications. First of all you should include two files dbg_leaks_dumper.h and dbg_leaks_dumper.cpp to your project. Then derive your class from MemoryLeaksDumpBase, override the dump_object_info() function and dump whatever you want. See the example of user defined type with dump capabilities below:

#include "dbg_leaks_dumper.h"

class UserClass: public dbg::MemoryLeaksDumpBase
    void dump_object_info()

Add to the your main module such an initialization code:


Then use user defined new operator (in my example it is _NEW, defined in common.h) for memory allocations for all user defined types. And that's all. Here is main module from my example project:

// LeaksDumperDemo.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include "UserClass.h"
#include "dbg_leaks_dumper.h"

//INIT_MEMORY_LEAKS_DUMPER(_USER_BLOCK); // Uncomment this line to dump only
                                         // _USER_BLOCKS

int _tmain(int argc, _TCHAR* argv[])
    UserClass* p = _NEW UserClass();

    return 0;

Now memory leaks info looks more convenient:

Detected memory leaks!
Dumping objects ->
e:\net projects\leaksdumperdemo\leaksdumperdemo\leaksdumperdemo.cpp(13) : 
    {102} client block at 0x00359F30, subtype 2, 4 bytes long.
Object dump complete.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Sergey Osenny
Team Leader Genesyslab
Ukraine Ukraine
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 11 Apr 2007
Article Copyright 2007 by Sergey Osenny
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid