Boost logo

Boost :

From: Andrzej Krzemienski (akrzemi1_at_[hidden])
Date: 2021-09-21 15:00:41


wt., 21 wrz 2021 o 16:50 Vinnie Falco via Boost <boost_at_[hidden]>
napisał(a):

> On Tue, Sep 21, 2021 at 7:34 AM Andrzej Krzemienski via Boost
> <boost_at_[hidden]> wrote:
> > I am not trying to solve any specific problem. It just strikes me that a
> > shared_ptr is used in a demo example for the library. I was always of the
> > opinion that a shared_ptr is often abused as a way of doing an ad-hoc GC.
>
> In theory it should work since the echo protocol is half-duplex. What
> happens when you switch it to unique_ptr? Move-only handlers should
> work, but it is possible that Chris missed a place that is still doing
> a copy.
>

It breaks when I pass a callback (completion handler), for instance in:

  void do_read()
  {
    auto self(shared_from_this());
    socket_.async_read_some(boost::asio::buffer(data_, max_length),
        [this, *self*](boost::system::error_code ec, std::size_t length)
        {
          if (!ec)
          {
            do_write(length);
          }
        });
  }

I would need to move the data inside the lambda capture, but if I do
it, the subsequent call to socket_.async_read_some() is UB.

In order for this to work, the function `async_read_some()` would have
to pass the socket back to my handler after it has performed the read.

Regards,
&rzej;


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk