That is because the string is not just passed as the first argument to async_send_to()
, but it is also used in the bind()
expression that is being passed to async_send_to()
as the third argument.
Function handle_send()
expects a shared_ptr
to a string
. Since the call is asynchronous, a string
object with automatic storage duration may have fallen out of scope and get destroyed by the time handle_send()
gets executed. Hence, the use of a shared_ptr
.