On Mon, 15 Jul 2019 at 13:35, JH via Boost
Hi,
I use boost TCP socket for months, it works well until I call a termination of smart pointer which is running at boost::asio::async_read. Could anyone explain what could be the cause for the following errors?
void Comms::HandleReadBody(const boost::system::error_code &error, const uint32_t length) { ................ boost::asio::async_read(this->mHandler->Socket(), boost::asio::buffer(this->mHeader, sizeof(this->mHeader)), [=](const boost::system::error_code &status, const uint32_t length) {this->HandleReadHeader(status, length);}); ............... }
You need to guarantee "this" still lives when the callback runs. You probably want to capture a shared ptr of "this" rather than "this" here. enable_shared_from_this is typically used for these scenarios.