This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Deleting STL heap objects across threads
- From: "Prakash Prabhu" <prakash dot prabhu at gmail dot com>
- To: libstdc++ at gcc dot gnu dot org
- Date: Thu, 7 Aug 2008 23:27:30 -0400
- Subject: Deleting STL heap objects across threads
Hi,
I am trying to pass an STL object (std::map) that is allocated on the
heap by one thread to another thread which after processing it,
deallocates it. However, in my current compilation configuration, this
seems to be seg faulting. If I try to delete objects allocated in the
thread which allocated the object, the program runs fine. I am linking
with pthread and using g++ 4.2.3 on Ubuntu 8.04. Maybe I am missing
something here (some linking/compilation flag or maybe a specific STL
allocator ?) , any pointers will be greatly appreciated.
Here is the example program:
#include <string>
#include <pthread.h>
#include <map>
using namespace std;
void *f(void *arg)
{
std::map<int, string, std::less<int> >* kk = (std::map<int, string,
std::less<int> >*)(kk);
delete kk;
}
void mt_test()
{
pthread_t thread;
void* ret;
std::map<int, string, std::less<int> >* kk = new std::map<int,
string, std::less<int> >();
pthread_create(&thread, NULL, f, (void*)kk);
pthread_join(thread, &ret);
// delete kk; // Line 2
}
int main()
{
mt_test();
return 0;
}
$ g++ --version
g++ (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu7)
...
$ g++ -o t t.cpp -pthread
$ gdb ./t
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
(gdb) run
Starting program: /home/pprabhu/trial/t
[Thread debugging using libthread_db enabled]
[New Thread 0xb7c408d0 (LWP 939)]
[New Thread 0xb7c3fb90 (LWP 944)]
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb7c3fb90 (LWP 944)]
0x08048856 in std::_Rb_tree<int, std::pair<int const, std::string>,
std::_Select1st<std::pair<int const, std::string> >, std::less<int>,
std::allocator<std::pair<int const, std::string> > >::_M_begin ()
Current language: auto; currently asm
(gdb) bt
#0 0x08048856 in std::_Rb_tree<int, std::pair<int const,
std::string>, std::_Select1st<std::pair<int const, std::string> >,
std::less<int>, std::allocator<std::pair<int const, std::string> >
>::_M_begin ()
#1 0x08048b96 in std::_Rb_tree<int, std::pair<int const,
std::string>, std::_Select1st<std::pair<int const, std::string> >,
std::less<int>, std::allocator<std::pair<int const, std::string> >
>::~_Rb_tree ()
#2 0x08048bf5 in std::map<int, std::string, std::less<int>,
std::allocator<std::pair<int const, std::string> > >::~map ()
#3 0x0804881b in f ()
#4 0xb7d954fb in start_thread () from /lib/tls/i686/cmov/libpthread.so.0
#5 0xb7d17e5e in clone () from /lib/tls/i686/cmov/libc.so.6
and the
$ sudo aptitude search libstdc++
v libstdc++-dev
-
i A libstdc++5
- The GNU Standard C++ Library v3
i libstdc++6
- The GNU Standard C++ Library v3
i A libstdc++6-4.2-dev
- The GNU Standard C++ Library v3
(development files)
thanks,
Prakash