diff --git a/doomsday/apps/client/src/main_client.cpp b/doomsday/apps/client/src/main_client.cpp index 433b942f65..57376cf649 100644 --- a/doomsday/apps/client/src/main_client.cpp +++ b/doomsday/apps/client/src/main_client.cpp @@ -83,8 +83,17 @@ int main(int argc, char** argv) } } - // Check that all reference-counted objects have been deleted. + // Check that all reference-counted objects have been deleted. +#ifdef DENG2_DEBUG +# ifdef DENG_USE_COUNTED_TRACING + if(de::Counted::totalCount > 0) + { + de::Counted::printAllocs(); + } +# else DENG2_ASSERT(de::Counted::totalCount == 0); +# endif +#endif return exitCode; } diff --git a/doomsday/cmake/Options.cmake b/doomsday/cmake/Options.cmake index a444623d54..a2b9c02b8e 100644 --- a/doomsday/cmake/Options.cmake +++ b/doomsday/cmake/Options.cmake @@ -27,3 +27,11 @@ option (DENG_FAKE_MEMORY_ZONE if (DENG_FAKE_MEMORY_ZONE) add_definitions (-DLIBDENG_FAKE_MEMORY_ZONE) endif () + +option (DENG_ENABLE_COUNTED_TRACING + "(Debug) Keep track of where de::Counted objects are allocated" + OFF +) +if (DENG_ENABLE_COUNTED_TRACING) + add_definitions (-DDENG_USE_COUNTED_TRACING) +endif () diff --git a/doomsday/sdk/libcore/include/de/data/counted.h b/doomsday/sdk/libcore/include/de/data/counted.h index 92eca63bc6..97c913a306 100644 --- a/doomsday/sdk/libcore/include/de/data/counted.h +++ b/doomsday/sdk/libcore/include/de/data/counted.h @@ -95,6 +95,9 @@ class DENG2_PUBLIC Counted #ifdef DENG2_DEBUG public: static int totalCount; // Number of Counted objects in existence. +# ifdef DENG_USE_COUNTED_TRACING + static void printAllocs(); +# endif #endif }; diff --git a/doomsday/sdk/libcore/src/data/counted.cpp b/doomsday/sdk/libcore/src/data/counted.cpp index 9deb200268..da91592dda 100644 --- a/doomsday/sdk/libcore/src/data/counted.cpp +++ b/doomsday/sdk/libcore/src/data/counted.cpp @@ -23,12 +23,29 @@ namespace de { #ifdef DENG2_DEBUG int Counted::totalCount = 0; ///< Should return back to zero when program ends. +# ifdef DENG_USE_COUNTED_TRACING +static QHash countedAllocs; +void Counted::printAllocs() +{ + qDebug() << "Counted objects:" << Counted::totalCount; + for(auto i = countedAllocs.constBegin(); i != countedAllocs.constEnd(); ++i) + { + qDebug() << "-=- Object" << i.key(); + qDebug() << i.value(); + } +} +# endif #endif Counted::Counted() : _refCount(1) { #ifdef DENG2_DEBUG totalCount++; +# ifdef DENG_USE_COUNTED_TRACING + QString trace; + DENG2_BACKTRACE(32, trace); + countedAllocs[this] = trace.toLatin1(); +# endif #endif } @@ -36,8 +53,10 @@ Counted::~Counted() { #ifdef DENG2_DEBUG totalCount--; +# ifdef DENG_USE_COUNTED_TRACING + countedAllocs.remove(this); +# endif #endif - //qDebug() << "~Counted" << this << typeid(*this).name() << "refCount:" << _refCount; DENG2_ASSERT(_refCount == 0); }