There is never a copy of
io_service (or the more recent
io_context that replaces the deprecated
io_service). That’s because it’s not copyable.
If you don’t maintain the lifetime while async operations have not completed, the behaviour is indeed undefined. However, it’s not too hard to make sure no async operations/completions happen by stopping the service forcibly gracefully awaiting for it to complete any pending operations.
I suggest the latter approach for production quality code.
Read more here: Source link