Boost logo

Boost Users :

Subject: [Boost-users] [Asio] Is shared_ptr with async_send_to ok?
From: Dylan Klomparens (dylan.klomparens_at_[hidden])
Date: 2009-11-10 16:39:28


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!



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net