Boost logo

Boost :

From: Oliver.Kowalke_at_[hidden]
Date: 2007-09-11 04:10:34


Hello,
I got an segmentation fault if I create mutliple threads and each thread
class a free function f() which creates an ptree from a xml stream.
Because each thread calls f() it should create its own ptree xml parser
- but why does the seg fault happen?

Regards, Oliver

Code:

#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>

#include <boost/bind.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/thread/barrier.hpp>

namespace ptr = boost::property_tree;

void f()
{
        try
        {
                std::string value = "<?xml version=\"1.0\"
encoding=\"utf-8\"?>\
        <fibonnaci>\
                <in>\
                        <number>10</number>\
                        <delay>1</delay>\
                </in>\
        </fibonnaci>";
                std::stringstream as_xml( value);
// printf("in data == %s\n", as_xml.str().c_str() );
                ptr::ptree p;
                ptr::read_xml( as_xml, p);
        }
        catch ( std::exception const& e)
        { std::cerr << e.what() << std::endl; }
        catch ( ... )
        { std::cerr << "unhandled" << std::endl; }
}

struct X
{
        boost::barrier & b_;

        X( boost::barrier & b)
        : b_( b)
        {}

        void operator()()
        {
                b_.wait();
                f();
        }
};

int main(int argc, char *argv[])
{
        try
        {
                boost::barrier b( 10);
                boost::thread_group tg;
                X x( b);
                tg.create_thread(
                        boost::bind(
                                & X::operator(),
                                x) );
                tg.create_thread(
                        boost::bind(
                                & X::operator(),
                                x) );
                tg.create_thread(
                        boost::bind(
                                & X::operator(),
                                x) );
                tg.create_thread(
                        boost::bind(
                                & X::operator(),
                                x) );
                tg.create_thread(
                        boost::bind(
                                & X::operator(),
                                x) );
                tg.create_thread(
                        boost::bind(
                                & X::operator(),
                                x) );
                tg.create_thread(
                        boost::bind(
                                & X::operator(),
                                x) );
                tg.create_thread(
                        boost::bind(
                                & X::operator(),
                                x) );
                tg.create_thread(
                        boost::bind(
                                & X::operator(),
                                x) );
                tg.create_thread(
                        boost::bind(
                                & X::operator(),
                                x) );
                tg.join_all();
        }
        catch ( std::exception const& e)
        { std::cerr << e.what() << std::endl; }
        catch ( ... )
        { std::cerr << "unhandled" << std::endl; }
}

DGB:

Program terminated with signal 11, Segmentation fault.
#0 0x0804e9dc in std::mem_fun1_t<int,
boost::spirit::impl::grammar_helper_base<boost::spirit::grammar<boost::p
roperty_tree::xml_parser::xml_grammar<boost::property_tree::basic_ptree<
boost::property_tree::ptree_traits<char> > >,
boost::spirit::parser_context<boost::spirit::nil_t> > >,
boost::spirit::grammar<boost::property_tree::xml_parser::xml_grammar<boo
st::property_tree::basic_ptree<boost::property_tree::ptree_traits<char>
> >, boost::spirit::parser_context<boost::spirit::nil_t> >*>::operator()
(this=0xb554300c, __p=0x809aad8, __x=0xb5543134)
    at
/opt/gcc-4.0.3/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.
3/bits/stl_function.h:664
664 { return (__p->*_M_f)(__x); }
(gdb) bt
#0 0x0804e9dc in std::mem_fun1_t<int,
boost::spirit::impl::grammar_helper_base<boost::spirit::grammar<boost::p
roperty_tree::xml_parser::xml_grammar<boost::property_tree::basic_ptree<
boost::property_tree::ptree_traits<char> > >,
boost::spirit::parser_context<boost::spirit::nil_t> > >,
boost::spirit::grammar<boost::property_tree::xml_parser::xml_grammar<boo
st::property_tree::basic_ptree<boost::property_tree::ptree_traits<char>
> >, boost::spirit::parser_context<boost::spirit::nil_t> >*>::operator()
(this=0xb554300c, __p=0x809aad8, __x=0xb5543134)
    at
/opt/gcc-4.0.3/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.
3/bits/stl_function.h:664
#1 0x0804ea2a in std::binder2nd<std::mem_fun1_t<int,
boost::spirit::impl::grammar_helper_base<boost::spirit::grammar<boost::p
roperty_tree::xml_parser::xml_grammar<boost::property_tree::basic_ptree<
boost::property_tree::ptree_traits<char> > >,
boost::spirit::parser_context<boost::spirit::nil_t> > >,
boost::spirit::grammar<boost::property_tree::xml_parser::xml_grammar<boo
st::property_tree::basic_ptree<boost::property_tree::ptree_traits<char>
> >, boost::spirit::parser_context<boost::spirit::nil_t> >*>
>::operator() (this=0xb554300c,
    __x=@0x809bc80) at
/opt/gcc-4.0.3/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.
3/bits/stl_function.h:446
#2 0x0804f57a in
std::for_each<std::reverse_iterator<__gnu_cxx::__normal_iterator<boost::
spirit::impl::grammar_helper_base<boost::spirit::grammar<boost::property
_tree::xml_parser::xml_grammar<boost::property_tree::basic_ptree<boost::
property_tree::ptree_traits<char> > >,
boost::spirit::parser_context<boost::spirit::nil_t> > >**,
std::vector<boost::spirit::impl::grammar_helper_base<boost::spirit::gram
mar<boost::property_tree::xml_parser::xml_grammar<boost::property_tree::
basic_ptree<boost::property_tree::ptree_traits<char> > >,
boost::spirit::parser_context<boost::spirit::nil_t> > >*,
std::allocator<boost::spirit::impl::grammar_helper_base<boost::spirit::g
rammar<boost::property_tree::xml_parser::xml_grammar<boost::property_tre
e::basic_ptree<boost::property_tree::ptree_traits<char> > >,
boost::spirit::parser_context<boost::spirit::nil_t> > >*> > > >,
std::binder2nd<std::mem_fun1_t<int,
boost::spirit::impl::grammar_helper_base<boost::spirit::grammar<boost::p
roperty_tree::xml_parser::xml_grammar<boost::property_tree::basic_ptree<
boost::property_tree::ptree_traits<char> > >,
boost::spirit::parser_context<boost::spirit::nil_t> > >,
boost::spirit::grammar<boost::property_tree::xml_parser::xml_grammar<boo
st::property_tree::basic_ptree<boost::property_tree::ptree_traits<char>
> >, boost::spirit::parser_context<boost::spirit::nil_t> >*> > >
(__first=@0xb5543088,
    __last=@0xb5543084, __f=
 
{<std::unary_function<boost::spirit::impl::grammar_helper_base<boost::sp
irit::grammar<boost::property_tree::xml_parser::xml_grammar<boost::prope
rty_tree::basic_ptree<boost::property_tree::ptree_traits<char> > >,
boost::spirit::parser_context<boost::spirit::nil_t> > >*,int>> = {<No
data fields>}, op =
{<std::binary_function<boost::spirit::impl::grammar_helper_base<boost::s
pirit::grammar<boost::property_tree::xml_parser::xml_grammar<boost::prop
erty_tree::basic_ptree<boost::property_tree::ptree_traits<char> > >,
boost::spirit::parser_context<boost::spirit::nil_t> >
>*,boost::spirit::grammar<boost::property_tree::xml_parser::xml_grammar<
boost::property_tree::basic_ptree<boost::property_tree::ptree_traits<cha
r> > >, boost::spirit::parser_context<boost::spirit::nil_t> >*,int>> =
{<No data fields>}, _M_f = {__pfn = 0x1, __delta = 0}}, value =
0xb5543134}) at
/opt/gcc-4.0.3/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.
3/bits/stl_algo.h:158
#3 0x0804f68f in
boost::spirit::impl::grammar_destruct<boost::spirit::grammar<boost::prop
erty_tree::xml_parser::xml_grammar<boost::property_tree::basic_ptree<boo
st::property_tree::ptree_traits<char> > >,
boost::spirit::parser_context<boost::spirit::nil_t> > > (
    self=0xb5543134) at
/opt/boost/include/boost-1_34/boost/spirit/core/non_terminal/impl/gramma
r.ipp:307
#4 0x080613de in ~grammar (this=0xb5543134) at
/opt/boost/include/boost-1_34/boost/spirit/core/non_terminal/grammar.hpp
:50
#5 0x0806148b in ~xml_grammar (this=0xb5543134)
    at
/opt/boost/include/boost-1_34/boost/property_tree/detail/xml_parser_read
_spirit.hpp:132
#6 0x0806cc59 in
boost::property_tree::xml_parser::read_xml_internal<boost::property_tree
::basic_ptree<boost::property_tree::ptree_traits<char> > >
(stream=@0xb5543274, pt=@0xb5543330, flags=0, filename=@0xb5543240)
    at
/opt/boost/include/boost-1_34/boost/property_tree/detail/xml_parser_read
_spirit.hpp:724
#7 0x0806cd9e in
boost::property_tree::xml_parser::read_xml<boost::property_tree::basic_p
tree<boost::property_tree::ptree_traits<char> > > (stream=@0xb5543274,
pt=@0xb5543330, flags=0) at
/opt/boost/include/boost-1_34/boost/property_tree/xml_parser.hpp:38
#8 0x0804ae6e in f () at
/home/kowalke/Projects/test_ptree/src/test_ptree.cpp:29
#9 0x0806cde8 in X::operator() (this=0xb5543448) at
/home/kowalke/Projects/test_ptree/src/test_ptree.cpp:48
#10 0x0804f79e in boost::_mfi::mf0<void, X>::operator()
(this=0xb5543440, t=@0xb5543448)
    at /opt/boost/include/boost-1_34/boost/bind/mem_fn_template.hpp:68
#11 0x0805179e in boost::_bi::list1<boost::_bi::value<X>
>::operator()<boost::_mfi::mf0<void, X>, boost::_bi::list0>
(this=0xb5543448,
    f=@0xb5543440, a=@0xb55433b6) at
/opt/boost/include/boost-1_34/boost/bind.hpp:231
#12 0x080517e1 in boost::_bi::bind_t<void, boost::_mfi::mf0<void, X>,
boost::_bi::list1<boost::_bi::value<X> > >::operator() (
    this=0xb5543440) at
/opt/boost/include/boost-1_34/boost/bind/bind_template.hpp:20
#13 0x080517fb in
boost::detail::function::void_function_obj_invoker0<boost::_bi::bind_t<v
oid, boost::_mfi::mf0<void, X>, boost::_bi::list1<boost::_bi::value<X> >
>, void>::invoke (function_obj_ptr=@0xb5543440)
    at
/opt/boost/include/boost-1_34/boost/function/function_template.hpp:155
#14 0xb7fad10c in boost::function0<void,
std::allocator<boost::function_base> >::operator() ()
   from /opt/boost/lib/libboost_thread-gcc40-mt-1_34.so.1.34.0
#15 0xb7fad061 in boost::thread_group::create_thread () from
/opt/boost/lib/libboost_thread-gcc40-mt-1_34.so.1.34.0
#16 0xb7d4f240 in start_thread () from
/lib/tls/i686/cmov/libpthread.so.0
#17 0xb7e313de in clone () from /lib/tls/i686/cmov/libc.so.6


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk