async_write(*this, BoostAsio::buffer(pck->_storage), boost::bind(&tcp_socket::handle_wrote, this, pck, BoostAsio::placeholders::error));
pck is allocated on heap, if I delete it, would 开发者_运维百科_storage become invalid as well or does async_write copy the buffer into its internal structures and it can be freely deleted/allocated on stack?
Thank you.
The async_write
function does not copy the buffer. It passes an internal const_buffer
object to deeper library functions. The const_buffer
object contains an internal pointer to the actual buffer, so no deep copy is performed. You need to make sure that pck->storage
remains valid until after the asynchronous handler is invoked.
Note that this is certainly for the best. Copying the buffer every time would be a really big performance drain.
Note the buffer or at least some of it will be copied into the kernel's socket buffer. Usually this is not a big deal. However You can set the send buffer to zero to use the application's buffer during the operation. Of course you shouldn't do that without understanding the other consequences of disabling the send buffer.
精彩评论