
I've got a class that's using ASIO to run a simple server listening for command messages from a remote interface. It's got some private members to help with this (class abridged for shortness): class ClassifierContext { private: io_service ioService_; t_socket_shptr socket_; }; It runs it's event loop in it's own thread (started with boost::thread) // Hosts thread to run IO event loop void ClassifierContext::io_loop() { try { connect(); } catch (std::exception &e) { RERROR("Error connecting to site server -- %s", e.what()); } // Work object prevents run() from stopping when it runs out of work asio::io_service::work work(ioService_); ioService_.run(); } And it periodically sends data back along a single socket associated with the ioService and also has an asynchronous read pending to listen for command messages (I can post this code if needed, it's just a couple async_sends and an async_read) The problem I'm seeing comes when the destructor for the class is run: ClassifierContext::~ClassifierContext() { ioService_.stop(); } Periodically (every 20 times or so) I get a segfault when the destructor for ioService_ is called. A representative stack trace looks like this: #0 0x0921bfd6 in ?? () #1 0x08125877 in boost::asio::detail::reactor_op_queue<int>::destroy_operations ( this=0x8ec5f40) at opt/linux/include/boost/asio/detail/reactor_op_queue.hpp:268 #2 0x081274ad in boost::asio::detail::epoll_reactor<false>::shutdown_service ( this=0x8ec5ed8) at opt/linux/include/boost/asio/detail/epoll_reactor.hpp:119 #3 0x08127b3b in ~service_registry (this=0x8ec5958) at opt/linux/include/boost/asio/detail/service_registry.hpp:75 #4 0x08127c29 in ~io_service (this=0x8492360) at opt/linux/include/boost/asio/impl/io_service.ipp:62 #5 0x08117110 in ~ClassifierContext (this=0x8492360) at ClassifierContext.cpp:91 #6 0xb781cbb9 in exit () from /lib/tls/i686/cmov/libc.so.6 #7 0xb780477d in __libc_start_main () from /lib/tls/i686/cmov/libc.so.6 #8 0x081079c1 in _start () at ../sysdeps/i386/elf/start.S:119 Has anyone seen a segfault like this before? It's intermittent so that tells me there's some kind of race condition. Any idea how I can work around it?