Boost logo

Threads-Devel :

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


Thanks Anthony,

I added "-pthread" while compilation as well as while linking, I am still getting segfaults.

# g++ -c -pthread -I. -I/usr/local/include/boost-1_38 -I/usr/include testThreadException.cpp -o testThreadException.o
# g++ -pthread testThreadException.o -static -L/u/test/lib -lboost_thread-gcc41-mt -lpthread -o testThreadException

Regards,

NG

====================================================

On 23/03/10 06:08, Neetu Garg wrote:
> 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

With gcc you often need the "-pthread" option to the compiler to ensure
that it compiles the code in a way that can handle multiple threads. Try
adding "-pthread" to both g++ commands.

Anthony

-- 
Author of C++ Concurrency in Action     http://www.stdthread.co.uk/book/
just::thread C++0x thread library             http://www.stdthread.co.uk
Just Software Solutions Ltd       http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
----- Original Message ----
From: Neetu Garg <garg_neetu_at_[hidden]>
To: threads-devel_at_[hidden]
Sent: Mon, March 22, 2010 11:08:37 PM
Subject: statically  linked example on Linux segfaults when exception is thrown
Hi,
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()
{
    try
    {
        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);
        nodeThreadGrp.join();
        std::cout << "DONE";
        getchar();
        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
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
=================================================================
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/libthread_db.so.1".
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++/eh_globals.cc:58
58        get_global() throw()
(gdb) backtrace
#0  __gnu_internal::get_global () at /usr/software/src/gcc-4.1.1/libstdc++-v3/libsupc++/eh_globals.cc:58
#1  0x080a2527 in __cxa_get_globals () at /usr/software/src/gcc-4.1.1/libstdc++-v3/libsupc++/eh_globals.cc:71
#2  0x080a20ff in __cxa_allocate_exception (thrown_size=8) at /usr/software/src/gcc-4.1.1/libstdc++-v3/libsupc++/eh_alloc.cc:154
#3  0x080a5e3c in std::__throw_out_of_range (__s=0x810e121 "basic_string::substr")
    at /usr/software/src/gcc-4.1.1/libstdc++-v3/src/functexcept.cc:79
#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/functexcept.cc:57
#9  0x080df76a in clone ()
Current language:  auto; currently c++
Thanks so much for your help
Regards,
NG
      

Threads-Devel list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk