431

I ran across enable_shared_from_this while reading the Boost.Asio examples and after reading the documentation I am still lost for how this should correctly be used. Can someone please give me an example and explanation of when using this class makes sense.

1

6 Answers 6

441

It enables you to get a valid shared_ptr instance to this, when all you have is this. Without it, you would have no way of getting a shared_ptr to this, unless you already had one as a member. This example from the boost documentation for enable_shared_from_this:

class Y: public enable_shared_from_this<Y>
{
public:

    shared_ptr<Y> f()
    {
        return shared_from_this();
    }
}

int main()
{
    shared_ptr<Y> p(new Y);
    shared_ptr<Y> q = p->f();
    assert(p == q);
    assert(!(p < q || q < p)); // p and q must share ownership
}

The method f() returns a valid shared_ptr, even though it had no member instance. Note that you cannot simply do this:

class Y: public enable_shared_from_this<Y>
{
public:

    shared_ptr<Y> f()
    {
        return shared_ptr<Y>(this);
    }
}

The shared pointer that this returned will have a different reference count from the "proper" one, and one of them will end up losing and holding a dangling reference when the object is deleted.

enable_shared_from_this has become part of C++ 11 standard. You can also get it from there as well as from boost.

10
  • 248
    +1. The key point is that the "obvious" technique of just returning shared_ptr<Y>(this) is broken, because this winds up creating multiple distinct shared_ptr objects with separate reference counts. For this reason you must never create more than one shared_ptr from the same raw pointer. Apr 3, 2009 at 2:31
  • 8
    It should be noted that in C++11 and later, it is perfectly valid to use a std::shared_ptr constructor on a raw pointer if it inherits from std::enable_shared_from_this. I don' t know if Boost's semantics were updated to support this.
    – Matthew
    Oct 10, 2017 at 20:51
  • 7
    @MatthewHolder Do you have a quote for this? On cppreference.com I read "Constructing a std::shared_ptr for an object that is already managed by another std::shared_ptr will not consult the internally stored weak reference and thus will lead to undefined behavior." (en.cppreference.com/w/cpp/memory/enable_shared_from_this) May 1, 2018 at 14:01
  • 14
    Why can't you just do shared_ptr<Y> q = p?
    – Dan M.
    May 8, 2018 at 14:28
  • 3
    @DanM. You can, that's why this sample is not very useful. There are definitely use cases for it though. When there's no q and you need a p from inside the class. Jan 5, 2019 at 14:23
294

from Dr Dobbs article on weak pointers, I think this example is easier to understand (source: http://drdobbs.com/cpp/184402026):

...code like this won't work correctly:

int *ip = new int;
shared_ptr<int> sp1(ip);
shared_ptr<int> sp2(ip);

Neither of the two shared_ptr objects knows about the other, so both will try to release the resource when they are destroyed. That usually leads to problems.

Similarly, if a member function needs a shared_ptr object that owns the object that it's being called on, it can't just create an object on the fly:

struct S
{
  shared_ptr<S> dangerous()
  {
     return shared_ptr<S>(this);   // don't do this!
  }
};

int main()
{
   shared_ptr<S> sp1(new S);
   shared_ptr<S> sp2 = sp1->dangerous();
   return 0;
}

This code has the same problem as the earlier example, although in a more subtle form. When it is constructed, the shared_ptr object sp1 owns the newly allocated resource. The code inside the member function S::dangerous doesn't know about that shared_ptr object, so the shared_ptr object that it returns is distinct from sp1. Copying the new shared_ptr object to sp2 doesn't help; when sp2 goes out of scope, it will release the resource, and when sp1 goes out of scope, it will release the resource again.

The way to avoid this problem is to use the class template enable_shared_from_this. The template takes one template type argument, which is the name of the class that defines the managed resource. That class must, in turn, be derived publicly from the template; like this:

struct S : enable_shared_from_this<S>
{
  shared_ptr<S> not_dangerous()
  {
    return shared_from_this();
  }
};

int main()
{
   shared_ptr<S> sp1(new S);
   shared_ptr<S> sp2 = sp1->not_dangerous();
   return 0;
}

When you do this, keep in mind that the object on which you call shared_from_this must be owned by a shared_ptr object. This won't work:

int main()
{
   S *p = new S;
   shared_ptr<S> sp2 = p->not_dangerous();     // don't do this
}
7
39

Here's my explanation, from a nuts and bolts perspective (top answer didn't 'click' with me). *Note that this is the result of investigating the source for shared_ptr and enable_shared_from_this that comes with Visual Studio 2012. Perhaps other compilers implement enable_shared_from_this differently...*

enable_shared_from_this<T> adds a private weak_ptr<T> instance to T which holds the 'one true reference count' for the instance of T.

So, when you first create a shared_ptr<T> onto a new T*, that T*'s internal weak_ptr gets initialized with a refcount of 1. The new shared_ptr basically backs onto this weak_ptr.

T can then, in its methods, call shared_from_this to obtain an instance of shared_ptr<T> that backs onto the same internally stored reference count. This way, you always have one place where T*'s ref-count is stored rather than having multiple shared_ptr instances that don't know about each other, and each think they are the shared_ptr that is in charge of ref-counting T and deleting it when their ref-count reaches zero.

3
  • 2
    This is correct, and the really important part is So, when you first create... because that is a requirement (as you say the weak_ptr isn't initialized until you pass the objects pointer into a shared_ptr ctor!) and this requirement is where things can go horribly wrong if you are not careful. If you create no shared_ptr before calling shared_from_this you get UB - likewise if you create more than one shared_ptr you get UB too. You have to somehow make sure you create a shared_ptr exactly once.
    – AnorZaken
    Sep 1, 2016 at 16:12
  • 3
    In other words the whole idea of enable_shared_from_thisis brittle to begin with since the point is to be able to get a shared_ptr<T> from a T*, but in reality when you get a pointer T* t it is generally not safe to assume anything about it already being shared or not, and making the wrong guess is UB.
    – AnorZaken
    Sep 1, 2016 at 16:17
  • "internal weak_ptr gets initialized with a refcount of 1" weak ptr to T are non owning smart ptr to T. A weak ptr is a owning smart ref to enough information to make a owning ptr that is a "copy" of other owning ptr. A weak ptr has no ref count. It has access to a ref count, like all owning ref.
    – curiousguy
    Nov 28, 2017 at 23:43
23

There is one particular case where I find enable_shared_from_this extremely useful: Thread safety when using asynchronous callback.

Imagine class Client has a member of type AsynchronousPeriodicTimer:

struct AsynchronousPeriodicTimer
{
    // call this periodically on some thread...
    void SetCallback(std::function<void(void)> callback); 
    void ClearCallback(); // clears the callback
}

struct Client
{
    Client(std::shared_ptr< AsynchronousPeriodicTimer> timer) 
        : _timer(timer)

    {
        _timer->SetCallback(
            [this]
            () 
            {
                assert(this); // what if 'this' is already dead because ~Client() has been called?
                std::cout << ++_counter << '\n';
            }
            );
    }
    ~Client()
    {
        // clearing the callback is not in sync with the timer, and can actually occur while the callback code is running
        _timer->ClearCallback();
    }
    int _counter = 0;
    std::shared_ptr< AsynchronousPeriodicTimer> _timer;
}

int main()
{
    auto timer = std::make_shared<AsynchronousPeriodicTimer>();
    {
        auto client = std::make_shared<Client>(timer);
        // .. some code    
        // client dies here, there is a race between the client callback and the client destructor           
    }
}

The client class subscribes a callback function to the periodic timer. Once the client object goes out of scope, there is a race condition between the client's callback and the client's destructor. The callback may be invoked with a dangling pointer!

The solution: using enable_shared_from_this to extend the object lifetime for the duration of the callback invocation.

struct Client : std::enable_shared_from_this<Client>
{
Client(std::shared_ptr< AsynchronousPeriodicTimer> timer) 
    : _timer(timer)

    {

    }

    void Init()
    {
        auto captured_self = weak_from_this(); // weak_ptr to avoid cyclic references with shared_ptr

        _timer->SetCallback(
        [captured_self]
        () 
        {
            if (auto self = captured_self.lock())
            {
                // 'this' is guaranteed to be non-nullptr. we managed to promote captured_self to a shared_ptr           
                std::cout << ++self->_counter << '\n';
            }

        }
        );
    }
    ~Client()
    {
        // the destructor cannot be called while the callback is running. shared_ptr guarantees this
        _timer->ClearCallback();
    
    }
    int _counter = 0;
    std::shared_ptr< AsynchronousPeriodicTimer> _timer;
}

The mechanism of enable_shared_from_this, combined with the inherent thread safety of std::shared_ptr reference counting, enable us to ensure that the Client object cannot be destructed while the callback code is accessing its internal members.

Note that the Init method is separated from the constructor since the initialization process of enable_shared_from_this is not finalized until the constructor exits. Hence the extra method. It is generally unsafe to subscribe an asynchronous callback from within a constructor since the callback may access uninitialized fields.

7
  • In this specific example, I don't understand what is the added value of using enable_shared_from_this here, since the Client clears the timer callback in its destructor ?
    – Scylardor
    Sep 5, 2020 at 1:42
  • 1
    @Scylardor imagine that during the time the timer callback is running, the desturctor is invoked in the main thread. The callback may be access destroyed 'this'. The actual clearing of the callback is neither atomic nor in sync with the timer. Sep 5, 2020 at 8:02
  • 1
    Oh ok, thank you for clearing that up. I forgot the multithreading implications of this. It makes sense now. Great example !
    – Scylardor
    Sep 8, 2020 at 2:15
  • 1
    I would vote this as the best answer. It clearly addresses the question: WHY is enable_shared_from_this useful? Other answers only try to explain what enable_shared_from_this does.
    – cyb70289
    Mar 17, 2021 at 4:57
  • 1
    @cyb70289 note that I just fixed a small mistake. Glad it helped. Mar 17, 2021 at 14:31
5

It's exactly the same in c++11 and later: It is to enable the ability to return this as a shared pointer since this gives you a raw pointer.

in other word, it allows you to turn code like this

class Node {
public:
    Node* getParent const() {
        if (m_parent) {
            return m_parent;
        } else {
            return this;
        }
    }

private:

    Node * m_parent = nullptr;
};           

into this:

class Node : std::enable_shared_from_this<Node> {
public:
    std::shared_ptr<Node> getParent const() {
        std::shared_ptr<Node> parent = m_parent.lock();
        if (parent) {
            return parent;
        } else {
            return shared_from_this();
        }
    }

private:

    std::weak_ptr<Node> m_parent;
};           
2
  • This will only work if these objects are always managed by a shared_ptr. You might want to change the interface to make sure it's the case.
    – curiousguy
    Nov 28, 2017 at 23:47
  • 1
    You are absolutely correct @curiousguy. This goes without saying. I also like typedef-ing all of my shared_ptr to improve readability when defining my public APIs. For example, instead of std::shared_ptr<Node> getParent const(), I would normally expose it as NodePtr getParent const() instead. If you absolutely need access to the internal raw pointer (best example: dealing with a C library), there's std::shared_ptr<T>::get for that, which I hate mentioning because I've this raw pointer accessor used too many times for the wrong reason.
    – mchiasson
    Dec 10, 2017 at 15:49
4

Note that using a boost::intrusive_ptr does not suffer from this problem. This is often a more convenient way to get around this issue.

2
  • Yes, but enable_shared_from_this allows you to work with an API which specifically accepts shared_ptr<>. In my opinion, such an API is usually Doing It Wrong (as it's better to let something higher in the stack own the memory) but if you're forced to work with such an API, this is a good option.
    – cdunn2001
    Jun 5, 2013 at 14:55
  • 2
    Better to stay within the standard as much as you can.
    – Sergei
    Feb 27, 2019 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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