I'm running some tests that need asynchronous communication, and the underlying framework is Asio. Sometimes, a handler is kept in the processing loop even if the test has been tore down, for good reasons. But then, it is called after the targets are deleted.
The Test
class:
virtual void SetUp()
{
_client = new Client;
_server = new Server;
Service::run();
}
virtual void TearDown()
{
Service::stop();
delete _client;
delete _server;
}
The Service
class:
static void run()
{
_thread = new asio::thread(boost::bind(&asio::io_service::run, _service));
}
static void stop()
{
_service->stop();
_thread->join();
delete _thread;
_service->reset();
}
io_service::stop()
is non-blocking so it gets quite useless in my case. If I delete the io_service
object at the end of the function, the handler won't be called, but I'd like a better solution to force completion before the objects are deleted.
Note: the actual processing loop is done in a second thread, but it is joined in a io_service::stop()
wrapper, and the whole problem doesn't seem to be thread-related.
I'm using Asio (non-Boost) 1.4.5, but could consider upgrading (to get the io_service::stopped()
operation?).
EDIT: Add the io_service
wrapper code as it seems to be relevant according to the comments.