fix memleak
authorAndreas Monzner <andreas.monzner@multimedia-labs.de>
Fri, 12 Aug 2005 15:32:28 +0000 (15:32 +0000)
committerAndreas Monzner <andreas.monzner@multimedia-labs.de>
Fri, 12 Aug 2005 15:32:28 +0000 (15:32 +0000)
lib/base/thread.cpp
lib/base/thread.h

index fa09691a37162be4f7d664edef7142d5186ca99c..08ddc1fb94b07cc33b355006947ad2eee7888e57 100644 (file)
@@ -20,15 +20,21 @@ void *eThread::wrapper(void *ptr)
        p->thread();
        pthread_exit(0);
        pthread_cleanup_pop(0);
        p->thread();
        pthread_exit(0);
        pthread_cleanup_pop(0);
+       return 0;
 }
 
 eThread::eThread()
 }
 
 eThread::eThread()
-       :alive(0)
+       :the_thread(0), alive(0)
 {
 }
 
 void eThread::run( int prio, int policy )
 {
 {
 }
 
 void eThread::run( int prio, int policy )
 {
+       if (alive)
+       {
+               eDebug("thread already running !!");
+               return;
+       }
        pthread_attr_t attr;
        pthread_attr_init(&attr);
        if (prio||policy)
        pthread_attr_t attr;
        pthread_attr_init(&attr);
        if (prio||policy)
@@ -38,7 +44,12 @@ void eThread::run( int prio, int policy )
                pthread_attr_setschedpolicy(&attr, policy );
                pthread_attr_setschedparam(&attr, &p);
        }
                pthread_attr_setschedpolicy(&attr, policy );
                pthread_attr_setschedparam(&attr, &p);
        }
-       pthread_create(&the_thread, &attr, wrapper, this);
+       if ( pthread_create(&the_thread, &attr, wrapper, this) )
+       {
+               eDebug("couldn't create new thread");
+               return;
+       }
+       pthread_attr_destroy(&attr);
        usleep(1000);
        int timeout=20;
        while(!alive && timeout--)
        usleep(1000);
        int timeout=20;
        while(!alive && timeout--)
@@ -52,36 +63,26 @@ void eThread::run( int prio, int policy )
 
 eThread::~eThread()
 {
 
 eThread::~eThread()
 {
-       if ( alive )
-               kill();
+       kill();
 }
 
 void eThread::sendSignal(int sig)
 {
        if ( alive )
                pthread_kill( the_thread, sig );
 }
 
 void eThread::sendSignal(int sig)
 {
        if ( alive )
                pthread_kill( the_thread, sig );
-       else 
+       else
                eDebug("send signal to non running thread");
 }
 
                eDebug("send signal to non running thread");
 }
 
-void eThread::kill(bool hard)
+void eThread::kill(bool sendcancel)
 {
 {
-       if ( !alive )
-       {
-               eDebug("kill.. but thread don't running");
+       if (!the_thread)
                return;
                return;
-       }
-
-       if ( hard )
+       if ( alive && sendcancel )
        {
        {
-               eDebug("killing the thread...");
+               eDebug("send cancel to thread");
                pthread_cancel(the_thread);
                pthread_cancel(the_thread);
-               alive=0;
-       }
-       else
-       {
-               eDebug("waiting for thread shutdown...");
-               pthread_join(the_thread, 0);
-               eDebug("ok");
        }
        }
+       eDebug("thread joined %d", pthread_join(the_thread, 0));
+       the_thread=0;
 }
 }
index f71300579238c22b839f14e69d051e0a1776b30c..f6c750aff53894aabac7578042ff463437e7facd 100644 (file)
@@ -14,14 +14,14 @@ public:
        bool thread_running() { return alive; }
        eThread();
        virtual ~eThread();
        bool thread_running() { return alive; }
        eThread();
        virtual ~eThread();
-       
-       void run(int prio=0,int policy=0);
+
+       void run(int prio=0, int policy=0);
 
        virtual void thread()=0;
        virtual void thread_finished() { }
 
        virtual void thread()=0;
        virtual void thread_finished() { }
-
        void sendSignal(int sig);
        void sendSignal(int sig);
-       void kill(bool hard=false);
+
+       void kill(bool sendcancel=false);
 };
 
 #endif
 };
 
 #endif