[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>
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:
participants (1)
-
Steve Lorimer