
I'm using a UDP socket to send data asynchronously. I know I need to preserve the data in a buffer until the async write is complete. Can I use a shared_ptr to accomplish this? Basically, my question is, will async_send_tokeep a copy of my shared_ptr so that the data doesn't get deleted? Do I need to use a particular overload of the boost::asio::buffer function to ensure that a copy of the shared_ptr is stored? Here is an example: boost::asio::streambuf Buffer; // ... buffer is filled with data ... int BufferSize = Buffer.size(); boost::shared_ptr<unsigned char> SharedBuffer(new unsigned char [BufferSize]); Buffer.sgetn(*SharedBuffer, BufferSize); Buffer.consume(BufferSize); Socket.async_send_to(boost::asio::buffer(*SharedBuffer, BufferSize), DestinationEndpoint, boost::bind(&MyClass::OnPacketSent, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); // Shared buffer goes out of scope here, immediately!