bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Thread memory leak


From: Ian Campbell
Subject: Thread memory leak
Date: Tue, 17 Jun 2003 14:16:16 +0100

David et al,

I have recently ported my code to version 2 of the library (latest version
commoncpp2-1.0.12) and have come up with a memory leak in the Thread class.
When a subclass that extends Thread is created but not started, the memory
associated with the "priv" member is not deleted (this is the ThreadImpl
data) when the thread is deleted, the following simple code illustrates the
situation:

#include <cc++/thread.h>
#include <mpatrol.h> /* Leak tracer include */

using namespace ost;

class TestThread : public Thread {

        public:
                void run() {
                        while (1 == 1) {
                                this->sleep(1000);
                        }
                }

};


int main () {

        TestThread *a, *b, *c;

        a = new TestThread();
        b = new TestThread();
        c = new TestThread();

        a->start();

        delete a;
        delete b;
        delete c;

}

Here all data associated with "a" is deleted but not "b" and "c".
 
Using my favourite memory checker (www.cbmamiga.demon.co.uk/mpatrol) I get
the following leaked memory as you can see below:

top 5 unfreed memory entries in leak table:

       bytes   count  location
    --------  ------  --------
        8160       1  __pthread_initialize_manager
         424       2  __Q23ost6ThreadiUi                        <-------
ThreadImpl data * 2
         212       1  __Q23ost6Threadb
         128       1  pthread_setspecific
          24       1  __9streambufi
        8948       6  total

Any thoughts on how to avoid this, without the naive solution (starting
every thread)? I occasionally create thread objects without starting them in
some of my code for house keeping purposes, for example with STL containers
of threads.

Ian Campbell
Band-X Ltd.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]