Debug aid - instantiation counter


Nothing revolutionary or anything, but it’s proved useful for me…

After a ton of buggering memory leaks (usually from me forgetting to deleteAllChildren() in a few destructors) i made this simple handy little aid.

What it does is help you check that all your objects are being created and destroyed, and make it clear when a particular object is not being put to sleep.

This goes in a header you can include in your class files…

// if we're in DEBUG mode...

  #define	DEBUG_COUNTER			static int _debugCounter;
  #define   DC_INIT(className)	int className::_debugCounter = 0;

  #define	DC_CREATE(className)   DBG( T("Created instance ") + String(_debugCounter++) + T(" of ") + #className );
  #define	DC_DESTROY(className)   DBG( T("Destroyed instance ") + String(--_debugCounter) + T(" of ") + #className );

  #define DC_INIT(className)
  #define DC_CREATE(className)
  #define DC_DESTROY(className)

Then you just add the macros into your classes like this:


class Test





This makes your debug output display a message when an object is created/destroyed, along with a counter of how many there are now/left. It’s automatically omitted from a release build.


(oops, should have been in the useful tools forum! :oops: )


and now it is. :wink:


Hey, it’s a good idea :wink:

I didn’t know this function, I only use delete and delete [] (but I use Dev C++, so I’m totally blind for memory leaks)… What is its function ?


if you call it in a destructor of a component with child components, it simply removes and deletes all the children :). If you forget to do this or an elaborate equivalent, then the child components don’t get cleaned up so you’ll leak like a frightened child