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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With