Sergei Zobov | Blog Software and robotics engineer

Be careful of using smart pointers and async code (C++)


One day I’ve spent some time to debug very odd segfault, that was caused by careless using of std::shared_ptr and boost::asio’s callbacks. It was easy to fix, but difficult to find the root of a problem. Here is a small advice, how you can avoid this mistake.

Advice

Do not forget to make a shared pointer from this if you pass it in lambda or bind, especially if you will use it somehow asynchronously, e.g. with boost::asio. In case you’d pass it as a raw pointer, you can get a segmentation fault because object won’t be tracked by shared_ptr, and when callback will be executed the pointer on this will be invalid.

#include <functional>
#include <memory>
#include <boost/asio.hpp>

class WonderfulClass: public std::enable_shared_from_this<WonderfulClass>
{
    ...
    void badCallbackCreation()
    {
        // auto clbk = std::bind(&WonderfulClass::func, this); <-- Can cause a sigfault
        auto clbk = std::bind(&WonderfulClass::func,
                              this->enable_shared_from_this());
        this->asyncThingy.post(clbk);
    }

    void func()
    {
        this->foo.bar();
    }
    ...
};

Content