Skip to content

Commit

Permalink
Debug|Unix|libcore: Keeping track of where Counted objects are created
Browse files Browse the repository at this point in the history
If there are unreleased Counted objects when the client is shut down,
backtraces of their allocation are now printed.

libcore can only access the backtrace on Unix.
  • Loading branch information
skyjake committed Sep 19, 2015
1 parent f67f98b commit 001ffc4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
11 changes: 10 additions & 1 deletion doomsday/apps/client/src/main_client.cpp
Expand Up @@ -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;
}
8 changes: 8 additions & 0 deletions doomsday/cmake/Options.cmake
Expand Up @@ -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 ()
3 changes: 3 additions & 0 deletions doomsday/sdk/libcore/include/de/data/counted.h
Expand Up @@ -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
};

Expand Down
21 changes: 20 additions & 1 deletion doomsday/sdk/libcore/src/data/counted.cpp
Expand Up @@ -23,21 +23,40 @@ namespace de {

#ifdef DENG2_DEBUG
int Counted::totalCount = 0; ///< Should return back to zero when program ends.
# ifdef DENG_USE_COUNTED_TRACING
static QHash<void *, QByteArray> 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
}

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);
}
Expand Down

0 comments on commit 001ffc4

Please sign in to comment.