[asio]Proper way to restart io_service

I have an application using boost::asio (v 1.35.0) that creates a udp multicast socket and an acceptor using an io_service and then calls io_service.run(). Additional sockets are created from acceptor.async_accept. My code needs to stop all the sockets and then restart them at a later point. When stopping the sockets, I close all the sockets I opened before deleting them. I then call io_service.stop(). When I restart things, I call io_service.reset(), recreate the udp multicast and acceptor sockets on the io_service and then call io_service.run(). When the io_service starts up again, it seems to have old operations on the queue and it tries to process them. I will usually get either a "vector iterator not dereferencable" error from a buffer on a deleted class or an error in a deleted version of the class that was processing async_accept before I stopped. Is there something additional I should be doing to clear the queue in the io_service? Thanks, John

When the io_service starts up again, it seems to have old operations on the queue and it tries to process them. I will usually get either a "vector iterator not dereferencable" error from a buffer on a deleted class or an error in a deleted version of the class that was processing async_accept before I stopped. Is there something additional I should be doing to clear the queue in the io_service?
I think I've got this working now. I read the documentation some more and noticed the run method will exit when there's no more work. So I now close all the sockets I've opened and do a short timed_wait on the thread to let run finish. I also have to catch operation_aborted errors in my handlers as they get called after I close the sockets. It looks like the issues I was having stopping and restarting an io_service should be expected since the documentation states that calling stop "will cause the io_service.run() call to return as soon as possible, abandoning unfinished operations and without permitting ready handlers to be dispatched." Would there be a way to modify the io_service to give the user a way to clear those unfinished operations? Also, I had a tough time figuring out what the error codes meant in my handlers as they just come back as an integer value (i.e. 995 == operation_aborted). Is there a way to get more information on these? Thanks, John
participants (1)
-
Urberg, John