Boost logo

Boost Users :

Subject: Re: [Boost-users] Asio example 5 sagmentation fault
From: Alessandro Candini (candini_at_[hidden])
Date: 2011-09-01 03:03:53


On 31/08/11 21:10, Anthony Foiani wrote:
> Alessandro, greetings --
>
> Alessandro Candini<candini_at_[hidden]> writes:
>
>> I'm really interested in exemple 5 of boost asio:
>>
>> http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/tutorial/tuttimer5/src.html
>>
>> I've tried it with boost-1.47.0. I can compile without warnings using:
>>
>> g++ -Wall -pedantic -lboost_thread -lboost_system -o timer5 timer5.cpp
>>
>> but when I launch the executable it gives me a Segmentation fault.
>>
>> Commenting the code I found that the segmentation fault raise at the
>> 3rd line of main:
>>
>> boost::thread t(boost::bind(&boost::asio::io_service::run,&io));
>>
>> I'm trying on the following machine:
>> Linux office-007 2.6.38-11-generic #48-Ubuntu SMP Fri Jul 29 19:02:55
>> UTC 2011 x86_64 x86_64 x86_64 GNU/Linux
> Not exactly the same setup, but on a very current gcc+boost on a
> similar system, it seems to work fine:
>
> $ uname -a
> Linux foo 2.6.35.14-95.fc14.x86_64 #1 SMP \
> Tue Aug 16 21:01:58 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux
>
> $ g++ -v
> Using built-in specs.
> COLLECT_GCC=g++
> COLLECT_LTO_WRAPPER=/usr/local/gcc-4.6.1/libexec/gcc/x86_64-unknown-linux-gnu/4.6.1/lto-wrapper
> Target: x86_64-unknown-linux-gnu
> Configured with: ../gcc-4.6.1/configure --prefix=/usr/local/gcc-4.6.1 \
> --with-local-prefix=/usr/local/gcc-4.6.1/local \
> --enable-languages=c,c++ --enable-threads
> Thread model: posix
> gcc version 4.6.1 (GCC)
>
> $ g++ -Wall -pedantic -I/usr/local/boost/include \
> -o boost-asio-segv boost-asio-segv.cpp \
> -L /usr/local/boost/lib -lboost_thread -lboost_system
>
> $ export LD_LIBRARY_PATH=/usr/local/gcc/lib64:/usr/local/boost/lib
>
> $ ./boost-asio-segv
> Timer 1: 0
> Timer 2: 1
> Timer 1: 2
> Timer 2: 3
> Timer 1: 4
> Timer 2: 5
> Timer 1: 6
> Timer 2: 7
> Timer 1: 8
> Timer 2: 9
> Final count is 10
>
> $ ldd ./boost-asio-segv
> linux-vdso.so.1 => (0x00007fffefda7000)
> libboost_thread.so.1.47.0 => /usr/local/boost/lib/libboost_thread.so.1.47.0 (0x00007f7898f96000)
> libboost_system.so.1.47.0 => /usr/local/boost/lib/libboost_system.so.1.47.0 (0x00007f7898d92000)
> libstdc++.so.6 => /usr/local/gcc/lib64/libstdc++.so.6 (0x00007f7898a8e000)
> libm.so.6 => /lib64/libm.so.6 (0x0000003da1a00000)
> libgcc_s.so.1 => /usr/local/gcc/lib64/libgcc_s.so.1 (0x00007f789885a000)
> libc.so.6 => /lib64/libc.so.6 (0x0000003da1200000)
> libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003da1600000)
> librt.so.1 => /lib64/librt.so.1 (0x0000003da2200000)
> /lib64/ld-linux-x86-64.so.2 (0x0000003da0e00000)
>
> You might want to try using 'ldd' to make sure that it's pulling in a
> consistent set of libraries (no 32/64 mismatch, no boost version
> mismatch, etc).
>
> Finally, use 'gdb' to see what is in "io" just before the segv. Just
> looking at that line, I don't see how either of those addresses could
> be null, which would be the usual cause of a segv. Maybe something
> like:
>
> void * run_addr = static_cast< void *>(&boost::asio::io_service_run );
> void * io_addr = static_cast< void *>(&io );
>
> And see if either of those are null?
>
> If you have the resources, can you try building your own g++ and
> boost, to see if it works under that combination?
>
> If you still can't solve it, can you make sure to post the version of
> g++ that you're using?
>
>> "WARNING:This message contains confidential and/or proprietary
>> information which may be subject to privilege or immunity and which
>> is intended for use of its addressee only. Should you receive this
>> message in error, you are kindly requested to inform the sender and
>> to definitively remove it from any paper or electronic format."
> And this sort of thing is pretty useless / rude on a public mailing
> list, don't you think?
>
> Best regards,
> Tony
>
> p.s. I could also get the exact same source code to run under the
> "stock" install on up-to-date Fedora 14, which means g++ 4.5.1
> and boost 1.44.0:
>
> $ unset LD_LIBRARY_PATH
>
> $ g++ -Wall -pedantic -o boost-asio-segv boost-asio-segv.cpp \
> -lboost_thread-mt -lboost_system-mt
>
> $ ./boost-asio-segv
> Timer 1: 0
> Timer 2: 1
> Timer 1: 2
> Timer 2: 3
> Timer 1: 4
> Timer 2: 5
> Timer 1: 6
> Timer 2: 7
> Timer 1: 8
> Timer 2: 9
> Final count is 10
>
> $ rpm -q gcc boost-thread
> gcc-4.5.1-4.fc14.x86_64
> boost-thread-1.44.0-8.fc14.x86_64
I have gcc-4.5.2 instead of 4.6.1, but this is not the problem.
Now it works: the problem was indeed a link to old boost-1.42, not yet
removed from my system.

Here's my steps to get it work:
$ make
g++ -Wall -pedantic -I/usr/local/include/boost -L /usr/local/lib
-lboost_thread -lboost_system -o timer5 timer5.cpp

$ ldd timer5
     linux-vdso.so.1 => (0x00007ffff634e000)
     libboost_thread.so.1.47.0 =>
/usr/local/lib/libboost_thread.so.1.47.0 (0x00007fe590190000)
     libboost_system.so.1.47.0 =>
/usr/local/lib/libboost_system.so.1.47.0 (0x00007fe58ff8c000)
     libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6
(0x00007fe58fc85000)
     libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fe58fa00000)
     libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1
(0x00007fe58f7ea000)
     libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe58f455000)
     libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0
(0x00007fe58f237000)
     librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fe58f02f000)
     /lib64/ld-linux-x86-64.so.2 (0x00007fe5903dd000)

$ ./timer5
Timer 1: 0
Timer 2: 1
Timer 1: 2
Timer 2: 3
Timer 1: 4
Timer 2: 5
Timer 1: 6
Timer 2: 7
Timer 1: 8
Timer 2: 9
Final count is 10

Thank you so much!

-- 
Alessandro Candini
MEEO S.r.l.
Via Saragat 9
I-44122 Ferrara, Italy
Tel: +39 0532 1861501
Fax: +39 0532 1861637
http://www.meeo.it
========================================
"ATTENZIONE:le informazioni contenute in questo messaggio sono
da considerarsi confidenziali ed il loro utilizzo è riservato unicamente
al destinatario sopra indicato. Chi dovesse ricevere questo messaggio
per errore è tenuto ad informare il mittente ed a rimuoverlo
definitivamente da ogni supporto elettronico o cartaceo."
"WARNING:This message contains confidential and/or proprietary
information which may be subject to privilege or immunity and which
is intended for use of its addressee only. Should you receive this
message in error, you are kindly requested to inform the sender and
to definitively remove it from any paper or electronic format."

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net