Do I need to delete my buffer after sending it into boost::asio::buffer?
Sp I have a function like:
void WriteToUrl(const unsigned char *buf, int size)
{
boost::asio::write(s, boost::asio::buffer(buf, size));
}
开发者_StackOverflow社区
Do I need to delete buffer if I do not use it any where else? How do I need to clean after y function?
If I have understood you are always the ownership of the buffer 'buf'. With synchronous operations, as ::write, you can delete the buffer as soon as ::write finish. With asynchronous operations, as ::async_write, you need to give an asynchronous completion token (a callback) which will be called when the operation has been done. The you could delete the buf in this callback.
You don't have a pointer to the buffer that you could delete, even if you wanted to.
The input data from buf
is used to construct the Boost.Asio buffer
, and then the memory used in that construction is managed by the library. The write call blocks until all the data is written, so I imagine the data is not copied internally (but I am not sure about that).
You should of course perform any required cleanup on the buffer you gave to the constructor (buf
) when you are done with it.
精彩评论