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 DEBUG_COUNTER
#define DC_INIT(className)
#define DC_CREATE(className)
#define DC_DESTROY(className)
Then you just add the macros into your classes like this:
.header
class Test
{
private:
...
DEBUG_COUNTER;
public:
Test();
~Test();
...
};
.cpp
DC_INIT(Test);
Test::Test()
{
DC_CREATE(Test);
}
Test::~Test()
{
DC_DESTROY(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.
