This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

segv when thread exits after throw/catch of C++ exception


I have a simple C program that spawns a thread; the thread calls into a dlopen'd
shared library.  The shared library is in C++ and its lone entry point just does
a throw and catch before returning.  We return back into the thread and when the
thread exits it crashes.  This happens on at least x86, x86_64 and ia64 on both
SLES 9 and RHEL 4.  I originally posted this to a glibc list and Ulrich Drepper
said:

  That's a problem in libstdc++.  They use pthread_key_create to create a TSD
  key but never call pthread_key_delete if the libstdc++ DSO is unloaded.

Thanks
Ed


$ gcc -o threadtest threadtest.c -pthread -lpthread -ldl -g
$ g++ -shared -o libthrow.so throw.cpp -lstdc++ -g
$ export LD_LIBRARY_PATH=.
$ ./threadtest
thread done, program should exit
Segmentation fault

threadtest.c
********
#include <pthread.h>
#include <stdio.h>
#include <dlfcn.h>

int ThreadFn(void *arg)
{
    int (*throw_entry)(void);
    void *handle;

    handle = dlopen("libthrow.so", RTLD_NOW);
    throw_entry = dlsym(handle, "throw_entry");

    if (throw_entry() != 123) {
         printf("throw_entry() didn't return 123 as expected\n");
    } 

    dlclose(handle);

    printf("thread done, program should exit\n");
    return 0;
}

int main(int argc, char* argv[])
{
   pthread_t thread;
   void *ret;

   pthread_create(&thread, NULL, (void *(*)(void *))ThreadFn, 0);
   pthread_join(thread, &ret);

   return 0;
}
********


throw.cpp
********
extern "C" {
 int throw_entry(void);
};

class foo {};

int throw_entry(void)
{
    try
    {
        throw foo();
    }
    catch(foo)
    {
        return 123;
    }
    return 0;
}
********


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]