Boost logo

Boost Users :

Subject: [Boost-users] [asio] creating acceptor on the stack causes seg-fault
From: Steve Lorimer (steve.lorimer_at_[hidden])
Date: 2010-07-16 06:16:22

Hi folks, I have the following class:

using namespace boost::asio;

// basic tcp server
class server
    boost::thread thread;
    ip::tcp::acceptor acceptor;
    io_service io_svc;

    // tcp_srv_conn object which will become the local end-point of the next
incoming connection
    tcp_srv_conn_p new_conn;

    void loop(unsigned short port);
    void on_accept(const boost::system::error_code &err);

    virtual ~server();

    void serve(unsigned short port);
    void serve(const std::string &svc);
    void close();

and the constructor for server looks like this:

server::server() :
 new_conn(new tcp_srv_conn(io_svc))
{ }

If I create a server object on the stack:

server s;

I get a segmentation fault deep inside the asio internals (something to do
with a mutex), originating from initialising acceptor with my io_service
object: acceptor(io_svc)

Here is a stack trace of the core file:
Marked in red is (#6) my server constructor and (#7) the
basic_socket_acceptor constructor

Core was generated by `tcp_srv.debug -p 12'.
Program terminated with signal 11, Segmentation fault.
#0 0x00007f8de6c3ed21 in pthread_mutex_lock () from /lib64/
(gdb) backtrace
#0 0x00007f8de6c3ed21 in pthread_mutex_lock () from /lib64/
#1 0x0000000000421153 in boost::asio::detail::posix_mutex::lock (this=0x8)
at /usr/include/boost/asio/detail/posix_mutex.hpp:71
#2 0x0000000000422db7 in scoped_lock (this=0x7f8de58d7a70, m=...) at
#3 0x0000000000426e22 in
> (this=0x0) at /usr/include/boost/asio/detail/service_registry.hpp:94
#4 0x000000000042573a in
> (ios=...) at /usr/include/boost/asio/impl/io_service.ipp:195
#5 0x000000000042427d in basic_io_object (this=0x7f8de58d7bd0,
io_service=...) at /usr/include/boost/asio/basic_io_object.hpp:72
#6 0x0000000000422fd7 in basic_socket_acceptor (this=0x7f8de58d7bd0,
io_service=...) at /usr/include/boost/asio/basic_socket_acceptor.hpp:77
#7 0x000000000041fe63 in server (this=0x7f8de58d7b90) at src/tcp_srv.cpp:9
#8 0x000000000040d4d2 in app::start_here (this=0x7fffaab13b2f, ac=3,
av=0x7fffaab13cd8) at test/server_test.cpp:65

Am I doing something stupid? How should I be creating my io_service and
acceptor objects?


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at