Boost logo

Boost :

From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2019-07-16 09:25:03

On Mon, 15 Jul 2019 at 13:35, JH via Boost <boost_at_[hidden]> wrote:
> 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.

Boost list run by bdawes at, gregod at, cpdaniel at, john at