1

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?

1
  • 1
    Wouldn't you callback invoke undefined behaviour when you call foo() a second time before the callback of the first call was invoked? The unique_ptr would be reset and therefore the callback would be raised on a deleted object. Mar 7 '16 at 10:00
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.