Boost logo

Threads-Devel :

Subject: [Threads-devel] statically linked example on Linux segfaults when exception is thrown
From: Neetu Garg (garg_neetu_at_[hidden])
Date: 2010-03-23 02:08:37


I have a small sample program using thread API. When I try to link the program statically, the sample program segfaults if the exception is thrown

Here is how I compile and link the sample

g++ -c -I. -I/usr/local/include/boost-1_38 -I/usr/include testThreadException.cpp -o testThreadException.o

g++ testThreadException.o -static -L/u/test/lib -lboost_program_options-gcc41-mt -lboost_filesystem-gcc41-mt -lboost_thread-gcc41-mt -lboost_system-gcc41-mt -lnet7ssh -lbotan -ltar -lz -lpthread -lrt -o testThreadException


Here is the sample code

#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream>

using namespace std;

boost::mutex node_mutex;
void printMsg()
        boost::mutex::scoped_lock lock(node_mutex);
        string demo = "help";
        string tmp = demo.substr(5,7); //out of range should throw exception
    catch(std::exception& e)
        cerr << "exception\n";
int main()
        using ::boost::thread;
        thread nodeThreadGrp= thread(printMsg);
        std::cout << "DONE";
        return 0;


I am using following version of gcc:
# g++ --version
g++ (GCC) 4.1.1
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO


Here is the core dump

$ gdb ./testThreadException core.14083
GNU gdb 6.2 ($Change: 365983 $)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu"...Using host libthread_db library "/lib/".
gdb: flush symcache

warning: core file may not match specified executable file.
Core was generated by `./testThreadException'.
Program terminated with signal 11, Segmentation fault.
#0 __gnu_internal::get_global () at /usr/software/src/gcc-4.1.1/libstdc++-v3/libsupc++/
58 get_global() throw()
(gdb) backtrace
#0 __gnu_internal::get_global () at /usr/software/src/gcc-4.1.1/libstdc++-v3/libsupc++/
#1 0x080a2527 in __cxa_get_globals () at /usr/software/src/gcc-4.1.1/libstdc++-v3/libsupc++/
#2 0x080a20ff in __cxa_allocate_exception (thrown_size=8) at /usr/software/src/gcc-4.1.1/libstdc++-v3/libsupc++/
#3 0x080a5e3c in std::__throw_out_of_range (__s=0x810e121 "basic_string::substr")
    at /usr/software/src/gcc-4.1.1/libstdc++-v3/src/
#4 0x08083054 in std::string::substr (this=Variable "this" is not available.
) at basic_string.h:306
#5 0x080484af in printMsg ()
#6 0x08048a5f in boost::detail::thread_data<void (*)()>::run ()
#7 0x08049b3c in thread_proxy ()
#8 0x08050055 in pthread_start_thread () at /usr/software/src/gcc-4.1.1/libstdc++-v3/src/
#9 0x080df76a in clone ()
Current language: auto; currently c++

Thanks so much for your help



Threads-Devel list run by bdawes at, david.abrahams at, gregod at, cpdaniel at, john at