logging-log4cxx-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Eric Rosenquist" <e...@rosenquist.com>
Subject Memory leaks?
Date Fri, 13 Aug 2004 14:24:29 GMT
I'm using log4cxx in a C++ application I'm writing and it's great -
gives us a large measure of compatibility between our C++ and Java code.
I needed threading support in my own app and ideally wanted something
that worked on Linux and Windows.  Linux is the target platform, but
it's nice to be able to debug things on Windows with Visual Studio in
the early going.

Rather than cook up my own thread classes I ended up using the Runnable
and Thread classes from log4cxx. What I've noticed with both valgrind
and the VC++ memory leak detection is that every thread I create causes
a small leak.  The object getting lost is due to the MDC object
associated with each thread.  Thread::run() calls
MDC::setContext(parentMDCMap) which causes a new Map object to be
associated with the thread. The per-thread map never seems to get
deleted anywhere when the thread terminates.

I've added a call to MDC::clear() at the end of Thread::run() - that
seems to cure the leak nicely and so far I haven't encountered any ill

One other minor thing that valgrind was complaining about is the
StringTokenizer destructor. The member variable this->str is allocated
using an array version of "new" in the constructor, but the delete call
in the destructor did not use the array version of "delete".

The patches are trivial if anyone wants to try them out:

Index: src/stringtokenizer.cpp
RCS file: /home/cvspublic/logging-log4cxx/src/stringtokenizer.cpp,v
retrieving revision 1.6
diff -r1.6 stringtokenizer.cpp
< 	delete this->str;
> 	delete [] this->str;
Index: src/thread.cpp
RCS file: /home/cvspublic/logging-log4cxx/src/thread.cpp,v
retrieving revision 1.14
diff -r1.14 thread.cpp
> 	MDC::clear();


View raw message