[asio] creating acceptor on the stack causes seg-fault
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); public: server(); 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() : acceptor(io_svc), 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/libpthread.so.0 (gdb) backtrace #0 0x00007f8de6c3ed21 in pthread_mutex_lock () from /lib64/libpthread.so.0 #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 /usr/include/boost/asio/detail/scoped_lock.hpp:36 #3 0x0000000000426e22 in boost::asio::detail::service_registry::use_service<boost::asio::socket_acceptor_service<boost::asio::ip::tcp>
(this=0x0) at /usr/include/boost/asio/detail/service_registry.hpp:94 #4 0x000000000042573a in boost::asio::use_service<boost::asio::socket_acceptor_service<boost::asio::ip::tcp> (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? TIA Steve
D'oh! Sorry, I'm super lame - have to init the io_service object BEFORE you pass it to the acceptor! Change the order of declaration in the class and no seg-fault! :) On 16 July 2010 11:16, Steve Lorimer <steve.lorimer@gmail.com> wrote:
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);
public: server(); 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() : acceptor(io_svc), 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/libpthread.so.0 (gdb) backtrace #0 0x00007f8de6c3ed21 in pthread_mutex_lock () from /lib64/libpthread.so.0 #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 /usr/include/boost/asio/detail/scoped_lock.hpp:36 #3 0x0000000000426e22 in boost::asio::detail::service_registry::use_service<boost::asio::socket_acceptor_service<boost::asio::ip::tcp>
(this=0x0) at /usr/include/boost/asio/detail/service_registry.hpp:94 #4 0x000000000042573a in boost::asio::use_service<boost::asio::socket_acceptor_service<boost::asio::ip::tcp> (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?
TIA Steve
participants (1)
-
Steve Lorimer