C++ – Async I/O operations – proper way to avoid deleting object

asynchronous-programmingboostcc++11

Using async I/O operations from boost::asio I often need shared pointers (and enable_shared_from_this and shared_from_this in callbacks) to avoid deleting objects too early. I think that it could be done by keeping unique_ptr or just object (ownership) in class (as a member)

For example: Only foo method use sender.

1st (popular solution):

class C {
    public:
    void foo
    {
        std::shared_ptr<Sender> sender = std::make_shared<Sender>();
        sender->send(); 
        // class Sender use async_write
        // inheritance: enable_shared_from_this
        // callback in async operation created with argument shared_from_this
    }
};

Why nobody(?) use this solution:

class D {
public:
    void foo
    {
        sender.reset(new Sender);
        sender->send(); 
    }
private:
    std::unique_ptr<Sender> sender;
};

I know that sender will not be deleted to early. I have no shared_ptrs. I think it's good to avoid them because if callback in Sender class also use async operations I need another shared pointers etc. I think that class D is more friendly to read. But I wonder if it's a good style… and I always see solution with shared_ptrs in the net.
I know that sender is not deleted after doing his job. But is it a problem?

Best Answer

Why nobody(?) use this solution

When you pass a shared pointer to boost asio, it will be deleted when asio no longer has pending i/o on it, if you have no shared references to it in your client code.

With passing a raw pointer to asio, you will have to ensure you keep it in memory until all asynchronous operations are completed. If you do not, you risk invalidating the pointer (in client code) while asio is using that memory.