Force boost::asio::buffer to copy by value
Answer from boost-users mailing lists:
If it did, it would be completely useless as you do not have access to any of the buffers in your completion handler.
The way to use buffer() is to pass in references to storage that you guarantee the lifetime of in some other way.
You can either have it stored in an external object, or store it in `this' as you did, or by binding it into the completion handler function object itself.
void onComplete(shared_ptr<std::string> s, error_code const&, size_t)
{
// do stuff
}
void send(std::string const& messageData)
{
shared_ptr<std::string> s = make_shared<std::string>(messageData);
async_send(socket, boost::asio::buffer(*s),
boost::bind(&T::onSend, this, s, _1, _2));
}
This ensures that the lifetime of the buffer data is at least as long as the completion handler exists.