[omniORB] OmniORB3.01 - Reducing memory leaks - patch 2

Jeroen.Dobbelaere@MMR.be Jeroen.Dobbelaere@MMR.be
Fri, 15 Sep 2000 14:52:08 +0100


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_000_01C01F1C.23FD1F40
Content-Type: text/plain;
	charset="ISO-8859-1"

Hi,

this patch will fix 2 memory leaks, created when initializing thread
information.

The destructor will now cleanup when the last static omni_thread::init_t
object gets destructed.

Note :
1. The patch changes omnithread.h (it adds a destructor !)
    Because of this, programs that want to use this feature will have to use
the changed omnithread.h header and must be
    recompiled.
2. Only the nt implementation has been tested and is provided in
patch2.patch
3. patch2b.patch is provided to update the other architectures (they also
need a destructor for omni_thread::init_t)
   As I don't work with those architectures (for now), the implementation is
just what I think it should be.
   for posix.cc, I left it empty, because it looked kind of complicated. 
   Any way, the implementation is always in between #if 0 -- #endif,
providing an empty destructor for those architectures.

Greetings,
--
Jeroen Dobbelaere
Software Design Engineer
Micro-Matic Research <http://www.mmr.be>


 <<patch2.patch>>  <<patch2b.patch>> 

------_=_NextPart_000_01C01F1C.23FD1F40
Content-Type: application/octet-stream;
	name="patch2.patch"
Content-Disposition: attachment;
	filename="patch2.patch"

*** omni/include/omnithread.h	Fri Aug 18 15:09:22 2000
--- omni_test/include/omnithread.h	Fri Sep 15 13:55:53 2000
***************
*** 515,524 ****
--- 515,525 ----
  
      class _OMNITHREAD_NTDLL_ init_t {
  	static int count;
      public:
  	init_t(void);
+ 	~init_t();
      };
  
      friend class init_t;
  
  OMNI_THREAD_EXPOSE:
*** omni/src/lib/omnithread/nt.cc	Thu Sep 23 20:10:24 1999
--- omni_test/src/lib/omnithread/nt.cc	Fri Sep 15 14:03:17 2000
***************
*** 455,464 ****
--- 455,490 ----
  
      if (!SetThreadPriority(t->handle, nt_priority(PRIORITY_NORMAL)))
  	throw omni_thread_fatal(GetLastError());
  }
  
+ 
+ // destructor : cleanup omni_thread object when appropriate
+ omni_thread::init_t::~init_t()
+ {
+    --count;
+   if(count == 0)
+     {
+       DB(cerr << "omni_thread::~init: NT implementation destructing\n");
+ 
+       if(self_tls_index != 0xffffffff)
+ 	{
+ 	  omni_thread* t = (omni_thread*)TlsGetValue(self_tls_index);
+ 	  if(t != NULL)
+ 	    {
+ 	      TlsFree(self_tls_index);
+ 	      self_tls_index = 0xffffffff;
+ 
+ 	      delete t;
+ 	    }
+ 	}
+ 
+       delete next_id_mutex;
+       next_id_mutex = NULL;
+     }
+ }
+ 
  //
  // Wrapper for thread creation.
  //
  
  extern "C" 

------_=_NextPart_000_01C01F1C.23FD1F40
Content-Type: application/octet-stream;
	name="patch2b.patch"
Content-Disposition: attachment;
	filename="patch2b.patch"

*** omni/src/lib/omnithread/mach.cc	Thu Mar  2 17:41:34 2000
--- omni_test/src/lib/omnithread/mach.cc	Fri Sep 15 14:10:11 2000
***************
*** 300,309 ****
--- 300,325 ----
    DB(cerr << "initial thread " << t->id() << endl);
  
    cthread_set_data(t->mach_thread, (any_t)t);
  }
  
+ // Destruction
+ omni_thread::init_t::~init_t(void)
+ {
+ #if 0
+   // never tested, this may even not compile
+   if (--count == 0)
+     {
+       omni_thread* t = (omni_thread*)c_thead_get_data(cthread_self());
+ 
+       delete t;
+ 
+       delete next_id_mutex;
+       next_id_mutex = 0;
+     }
+ #endif
+ }
  
  //
  // Wrapper for thread creation.
  //
  
*** omni/src/lib/omnithread/posix.cc	Fri Aug 18 15:09:12 2000
--- omni_test/src/lib/omnithread/posix.cc	Fri Sep 15 14:12:18 2000
***************
*** 387,396 ****
--- 387,408 ----
  
  #endif   /* PthreadSupportThreadPriority */
  }
  
  
+ // destructor
+ omni_thread::init_t::~init_t(void)
+ {
+ #if 0 // need to be filled in...
+   if (--count == 0)
+     {
+       // get rid of all objects, created in init_t::init_t();
+       // TODO : provide a valid implementation
+     }
+ #endif
+ }
+ 
  //
  // Wrapper for thread creation.
  //
  
  extern "C" void* 
*** omni/src/lib/omnithread/solaris.cc	Thu Sep 23 20:10:26 1999
--- omni_test/src/lib/omnithread/solaris.cc	Fri Sep 15 14:16:01 2000
***************
*** 206,215 ****
--- 206,232 ----
      THROW_ERRORS(thr_setspecific(self_key, (void*)t));
  
      THROW_ERRORS(thr_setprio(t->sol_thread, sol_priority(PRIORITY_NORMAL)));
  }
  
+ // destructor
+ omni_thread::init_t::~init_t(void)
+ {
+ #if 0 // this may even not compile
+   if (--count == 0)
+     {
+       omni_thread* t = (omni_thread*) thr_getspecific(self_key);
+ 
+       thr_keyremove(self_key); // ??
+       delete t;
+ 
+ 
+       delete next_id_mutex;
+       next_id_mutex = 0;
+     }
+ #endif
+ }
  
  //
  // Wrapper for thread creation.
  //
  

------_=_NextPart_000_01C01F1C.23FD1F40--