When a thread is terminated from within an exception handling block, exception
object(s) (__cxa_exeption) are leaked, as the TSD destructor for __cxa_eh_globals
instance does delete them.  The following program demonstrates the leak.  A patch
and a ChangeLog entry follows.

#include <pthread.h>

static void *
foo (void *p)
{
  try
    {
      throw 0;
    }
  catch (int)
    {
      pthread_exit (0);
    }
}

int
main ()
{
  pthread_t t;
  void *p;

  pthread_create (&t, 0, foo, 0);
  pthread_join (t, &p);
  return 0;
}


--- /home/velco/src/gcc-4.0/libstdc++-v3/libsupc++/eh_globals.cc.~1.6.~ Tue Aug
 3 09:45:54 2004
+++ /home/velco/src/gcc-4.0/libstdc++-v3/libsupc++/eh_globals.cc        Wed Oct 27
20:50:17 2004
@@ -48,7 +48,19 @@
 get_globals_dtor (void *ptr)
 {
   if (ptr)
-    std::free (ptr);
+    {
+      __cxa_exception *exn, *next;
+
+      exn = ((__cxa_eh_globals *) ptr)->caughtExceptions;
+      while (exn)
+       {
+         next = exn->nextException;
+         _Unwind_DeleteException (&exn->unwindHeader);
+         exn = next;
+       }
+
+      std::free (ptr);
+    }
 }
 
 static void

2004-10-27  Momchil Velikov  <[EMAIL PROTECTED]>

        * libsupc++/eh_globals.cc (get_globals_dtor): Delete unhandled
        exceptions.

-- 
           Summary: Unhandled exceptions leak
           Product: gcc
           Version: 4.0.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: velco at fadata dot bg
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18185

Reply via email to