In short, boost::bind
creates a copy of the boost::shared_ptr<Connection>
that is returned from shared_from_this()
, and boost::asio
may create a copy of the handler. The copy of the handler will remain alive until one of the following occurs:
- The handler has been called by a thread from which the service’s
run()
,run_one()
,poll()
orpoll_one()
member function has been invoked. - The
io_service
is destroyed. - The
io_service::service
that owns the handler is shutdown viashutdown_service()
.
Here are the relevant excerpts from the documentation:
-
boost::bind documentation:
The arguments that
bind
takes are copied and held internally by the returned function object. -
boost::asio
io_service::post
:The
io_service
guarantees that the handler will only be called in a thread in which therun()
,run_one()
,poll()
orpoll_one()
member functions is currently being invoked. […] Theio_service
will make a copy of the handler object as required. -
boost::asio
io_service::~io_service
:Uninvoked handler objects that were scheduled for deferred invocation on the
io_service
, or any associated strand, are destroyed.Where an object’s lifetime is tied to the lifetime of a connection (or some other sequence of asynchronous operations), a
shared_ptr
to the object would be bound into the handlers for all asynchronous operations associated with it. […] When a single connection ends, all associated asynchronous operations complete. The corresponding handler objects are destroyed, and allshared_ptr
references to the objects are destroyed.
While dated (2007), the Networking Library Proposal for TR2 (Revision 1) was derived from Boost.Asio. Section 5.3.2.7. Requirements on asynchronous operations
provides some details for the arguments to async_
functions:
In this clause, an asynchronous operation is initiated by a function that is named with the prefix
async_
. These functions shall be known as initiating functions. […] The library implementation may make copies of the handler argument, and
the original handler argument and all copies are interchangeable.The lifetime of arguments to initiating functions shall be treated as follows:
- If the parameter is declared as a const reference or by-value […] the implementation may make copies of the argument, and all copies shall be destroyed no later than immediately after invocation of the handler.
[…] Any calls made by the library implementation to functions associated with the initiating function’s arguments will be performed such that calls occur in a sequence call1 to calln, where for all i, 1 ≤ i < n, calli precedes call i+1.
Thus:
- The implementation may create a copy of the handler. In the example, the copied handler will create a copy of the
shared_ptr<Connection>
, increasing the reference count of theConnection
instance while the copies of handler remain alive. - The implementation may destroy the handler prior to invoking handler. This occurs if the async operation is outstanding when
io_serive::service
is shutdown or theio_service
is destroyed. In the example, the copies of handler will be destroyed, decreasing the reference count ofConnection
, and potentially causing theConnection
instance to be destroyed. - If handler is invoked, then all copies of handler will immediately be destroyed once execution returns from the handler. Again, the copies of handler will be destroyed, decreasing the reference count of
Connection
, and potentially causing it to be destroyed. - The functions associated with the
asnyc_
‘s arguments, will be executed sequentially, and not concurrent. This includesio_handler_deallocate
andio_handler_invoke
. This guarantees that the handler will not be deallocated while the handler is being invoked. In most areas of theboost::asio
implementation, the handler is copied or moved to stack variables, allowing the destruction to occur once execution exits the block in which it was declared. In the example, this ensures that the reference count forConnection
will be at least one during the invocation of the handler.