boost::asio segmentation fault
Доброго всем времени суток.
Только начал изучать С++. Изучаю по Джесс Либерти: Освой самостоятельно C++ за 21 день. Но вот решил я посмотреть на досуге boost::asio. Взял пример с их сайта, компилирование проходит удачно:
root:[~/cpp]#gcc -Wall -g -I/usr/local/include -lboost_system -lstdc++ -v -o stream_server.o stream_server.cpp
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc5/gcc/i386-portbld-freebsd10.2/5.2.0/lto-wrapper
Target: i386-portbld-freebsd10.2
Configured with: ./../gcc-5.2.0/configure --with-build-config=bootstrap-debug --disable-nls --enable-gnu-indirect-function --libdir=/usr/local/lib/gcc5 --libexecdir=/usr/local/libexec/gcc5 --program-suffix=5 --with-as=/usr/local/bin/as --with-gmp=/usr/local --with-gxx-include-dir=/usr/local/lib/gcc5/include/c++/ --with-ld=/usr/local/bin/ld --with-pkgversion='FreeBSD Ports Collection' --with-system-zlib --with-ecj-jar=/usr/local/share/java/ecj-4.5.jar --enable-languages=c,c++,objc,fortran,java --prefix=/usr/local --localstatedir=/var --mandir=/usr/local/man --infodir=/usr/local/info/gcc5 --build=i386-portbld-freebsd10.2
Thread model: posix
gcc version 5.2.0 (FreeBSD Ports Collection)
COLLECT_GCC_OPTIONS='-Wall' '-g' '-I' '/usr/local/include' '-v' '-o' 'stream_server.o' '-mtune=generic' '-march=i486'
/usr/local/libexec/gcc5/gcc/i386-portbld-freebsd10.2/5.2.0/cc1plus -quiet -v -I /usr/local/include stream_server.cpp -quiet -dumpbase stream_server.cpp -mtune=generic -march=i486 -auxbase stream_server -g -Wall -version -o /tmp//cczIm72j.s
GNU C++ (FreeBSD Ports Collection) version 5.2.0 (i386-portbld-freebsd10.2)
compiled by GNU C version 5.2.0, GMP version 5.1.3, MPFR version 3.1.2, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/lib/gcc5/gcc/i386-portbld-freebsd10.2/5.2.0/../../../../../i386-portbld-freebsd10.2/include"
ignoring duplicate directory "/usr/local/include"
as it is a non-system directory that duplicates a system directory
#include "..." search starts here:
#include <...> search starts here:
/usr/local/lib/gcc5/include/c++/
/usr/local/lib/gcc5/include/c++//i386-portbld-freebsd10.2
/usr/local/lib/gcc5/include/c++//backward
/usr/local/lib/gcc5/gcc/i386-portbld-freebsd10.2/5.2.0/include
/usr/local/include
/usr/local/lib/gcc5/gcc/i386-portbld-freebsd10.2/5.2.0/include-fixed
/usr/include
End of search list.
GNU C++ (FreeBSD Ports Collection) version 5.2.0 (i386-portbld-freebsd10.2)
compiled by GNU C version 5.2.0, GMP version 5.1.3, MPFR version 3.1.2, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 40aa5b89d7a542ef5b3a90cf6a0756f7
COLLECT_GCC_OPTIONS='-Wall' '-g' '-I' '/usr/local/include' '-v' '-o' 'stream_server.o' '-mtune=generic' '-march=i486'
/usr/local/bin/as -v -I /usr/local/include -o /tmp//cc58vUYd.o /tmp//cczIm72j.s
GNU ассемблер, версия 2.25.1 (i386-portbld-freebsd10.2); используется BFD версии (GNU Binutils) 2.25.1
COMPILER_PATH=/usr/local/libexec/gcc5/gcc/i386-portbld-freebsd10.2/5.2.0/:/usr/local/libexec/gcc5/gcc/i386-portbld-freebsd10.2/5.2.0/:/usr/local/libexec/gcc5/gcc/i386-portbld-freebsd10.2/:/usr/local/lib/gcc5/gcc/i386-portbld-freebsd10.2/5.2.0/:/usr/local/lib/gcc5/gcc/i386-portbld-freebsd10.2/:/usr/local/lib/gcc5/gcc/i386-portbld-freebsd10.2/5.2.0/../../../../../i386-portbld-freebsd10.2/bin/
LIBRARY_PATH=/usr/local/lib/gcc5/gcc/i386-portbld-freebsd10.2/5.2.0/:/usr/local/lib/gcc5/gcc/i386-portbld-freebsd10.2/5.2.0/../../../../../i386-portbld-freebsd10.2/lib/:/usr/local/lib/gcc5/gcc/i386-portbld-freebsd10.2/5.2.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-Wall' '-g' '-I' '/usr/local/include' '-v' '-o' 'stream_server.o' '-mtune=generic' '-march=i486'
/usr/local/libexec/gcc5/gcc/i386-portbld-freebsd10.2/5.2.0/collect2 -plugin /usr/local/libexec/gcc5/gcc/i386-portbld-freebsd10.2/5.2.0/liblto_plugin.so -plugin-opt=/usr/local/libexec/gcc5/gcc/i386-portbld-freebsd10.2/5.2.0/lto-wrapper -plugin-opt=-fresolution=/tmp//ccyYC2y6.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --eh-frame-hdr -V -dynamic-linker /libexec/ld-elf.so.1 -o stream_server.o /usr/lib/crt1.o /usr/lib/crti.o /usr/local/lib/gcc5/gcc/i386-portbld-freebsd10.2/5.2.0/crtbegin.o -L/usr/local/lib/gcc5/gcc/i386-portbld-freebsd10.2/5.2.0 -L/usr/local/lib/gcc5/gcc/i386-portbld-freebsd10.2/5.2.0/../../../../../i386-portbld-freebsd10.2/lib -L/usr/local/lib/gcc5/gcc/i386-portbld-freebsd10.2/5.2.0/../../.. -lboost_system -lstdc++ /tmp//cc58vUYd.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/local/lib/gcc5/gcc/i386-portbld-freebsd10.2/5.2.0/crtend.o /usr/lib/crtn.o
GNU ld (GNU Binutils) 2.25.1
Supported emulations:
elf_i386_fbsd
elf_i386
i386bsd
Но при запуске уходит в SEGFAULT:
Starting program: /root/cpp/stream_server.o
[New Thread 28c08080 (LWP 100337)]
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 28c08080 (LWP 100337)]
0x00000000 in ?? ()
(gdb) bt
#0 0x00000000 in ?? ()
#1 0x280f50fb in __cxxabiv1::__dynamic_cast (src_ptr=0x281fdea0 <(anonymous namespace)::ctype_c>, src_type=0x281f7280 <typeinfo for std::locale::facet>, dst_type=0x281f7adc <typeinfo for std::ctype<char>>,
src2dst=0) at ../../.././../gcc-5.2.0/libstdc++-v3/libsupc++/dyncast.cc:72
#2 0x28174efa in std::has_facet<std::ctype<char> > (__loc=...) at /usr/ports/lang/gcc5/work/build/i386-portbld-freebsd10.2/libstdc++-v3/include/bits/locale_classes.tcc:110
#3 0x28167baf in std::basic_ios<char, std::char_traits<char> >::_M_cache_locale (this=0x281fd184 <std::cout+4>, __loc=...)
at /usr/ports/lang/gcc5/work/build/i386-portbld-freebsd10.2/libstdc++-v3/include/bits/basic_ios.tcc:159
#4 0x28167d4b in std::basic_ios<char, std::char_traits<char> >::init (this=0x281fd184 <std::cout+4>, __sb=0x281fcd80 <__gnu_internal::buf_cout_sync>)
at /usr/ports/lang/gcc5/work/build/i386-portbld-freebsd10.2/libstdc++-v3/include/bits/basic_ios.tcc:132
#5 0x2810c0e5 in basic_ostream (__sb=<optimized out>, this=<optimized out>, __in_chrg=<optimized out>, __vtt_parm=<optimized out>)
at /usr/ports/lang/gcc5/work/build/i386-portbld-freebsd10.2/libstdc++-v3/include/ostream:85
#6 std::ios_base::Init::Init (this=0x80603f4 <std::__ioinit>) at ../../../.././../gcc-5.2.0/libstdc++-v3/src/c++98/ios_init.cc:91
#7 0x0804a278 in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535) at /usr/local/lib/gcc5/include/c++/iostream:74
#8 0x0804a535 in _GLOBAL__sub_I_main () at stream_server.cpp:136
#9 0x28063715 in ?? () from /libexec/ld-elf.so.1
#10 0x28062ccf in ?? () from /libexec/ld-elf.so.1
#11 0x2806105e in ?? () from /libexec/ld-elf.so.1
Код примера:
//
// stream_server.cpp
// ~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <cstdio>
#include <iostream>
#include <boost/array.hpp>
#include <boost/bind.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/asio.hpp>
#if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
using boost::asio::local::stream_protocol;
class session
: public boost::enable_shared_from_this<session>
{
public:
session(boost::asio::io_service& io_service)
: socket_(io_service)
{
}
stream_protocol::socket& socket()
{
return socket_;
}
void start()
{
socket_.async_read_some(boost::asio::buffer(data_),
boost::bind(&session::handle_read,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void handle_read(const boost::system::error_code& error,
size_t bytes_transferred)
{
if (!error)
{
boost::asio::async_write(socket_,
boost::asio::buffer(data_, bytes_transferred),
boost::bind(&session::handle_write,
shared_from_this(),
boost::asio::placeholders::error));
}
}
void handle_write(const boost::system::error_code& error)
{
if (!error)
{
socket_.async_read_some(boost::asio::buffer(data_),
boost::bind(&session::handle_read,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
}
private:
// The socket used to communicate with the client.
stream_protocol::socket socket_;
// Buffer used to store data received from the client.
boost::array<char, 1024> data_;
};
typedef boost::shared_ptr<session> session_ptr;
class server
{
public:
server(boost::asio::io_service& io_service, const std::string& file)
: io_service_(io_service),
acceptor_(io_service, stream_protocol::endpoint(file))
{
session_ptr new_session(new session(io_service_));
acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error));
}
void handle_accept(session_ptr new_session,
const boost::system::error_code& error)
{
if (!error)
{
new_session->start();
new_session.reset(new session(io_service_));
acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error));
}
}
private:
boost::asio::io_service& io_service_;
stream_protocol::acceptor acceptor_;
};
int main(int argc, char* argv[])
{
try
{
if (argc != 2)
{
std::cerr << "Usage: stream_server <file>\n";
std::cerr << "*** WARNING: existing file is removed ***\n";
return 1;
}
boost::asio::io_service io_service;
std::remove(argv[1]);
server s(io_service, argv[1]);
io_service.run();
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}
#else // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
# error Local sockets not available on this platform.
#endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
Просьба помочь понять причину ошибки :(