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.
-
1The simple explanation is here: en.cppreference.com/w/cpp/memory/enable_shared_from_this– FedorJun 16, 2021 at 6:43
6 Answers
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.
-
243+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
-
7It 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 fromstd::enable_shared_from_this
. I don' t know if Boost's semantics were updated to support this.– MatthewOct 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 anotherstd::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
-
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 ap
from inside the class. Jan 5, 2019 at 14:23
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_pt
r 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
}
-
26Thanks, this illustrates the problem being solved better than the currently accepted answer. May 2, 2013 at 14:04
-
2+1: Good answer. As an aside, instead of
shared_ptr<S> sp1(new S);
it may be preferred to useshared_ptr<S> sp1 = make_shared<S>();
, see for example stackoverflow.com/questions/18301511/…– ArunApr 1, 2015 at 20:27 -
9I'm pretty sure the last line should read
shared_ptr<S> sp2 = p->not_dangerous();
because the pitfall here is that you must create a shared_ptr the normal way before you callshared_from_this()
the first time! This is really easy to get wrong! Before C++17 it is UB to callshared_from_this()
before exactly one shared_ptr has been created the normal way:auto sptr = std::make_shared<S>();
orshared_ptr<S> sptr(new S());
. Thankfully from C++17 onwards doing so will throw. Sep 1, 2016 at 15:48 -
2BAD Example:
S* s = new S(); shared_ptr<S> ptr = s->not_dangerous();
<-- It is permitted to call shared_from_this only on a previously shared object, i.e. on an object managed by std::shared_ptr<T>. Otherwise the behavior is undefined (until C++17)std::bad_weak_ptr is thrown (by the shared_ptr constructor from a default-constructed weak_this) (since C++17).. So the reality is that it should be calledalways_dangerous()
, because you need the knowledge of if it has been shared already or not. Sep 1, 2016 at 15:56 -
2@AnorZaken Good point. It would've been useful if you had submitted an edit request to make that fix. I've just done so. The other useful thing would've been for the poster not to choose subjective, context-sensitive method names! Dec 12, 2016 at 10:06
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.
-
2This 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 callingshared_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. Sep 1, 2016 at 16:12 -
3In other words the whole idea of
enable_shared_from_this
is brittle to begin with since the point is to be able to get ashared_ptr<T>
from aT*
, but in reality when you get a pointerT* t
it is generally not safe to assume anything about it already being shared or not, and making the wrong guess is UB. 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. Nov 28, 2017 at 23:43
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.
-
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 ? 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
-
1Oh ok, thank you for clearing that up. I forgot the multithreading implications of this. It makes sense now. Great example ! Sep 8, 2020 at 2:15
-
1I 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.– cyb70289Mar 17, 2021 at 4:57
-
1@cyb70289 note that I just fixed a small mistake. Glad it helped. Mar 17, 2021 at 14:31
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.
-
Yes, but
enable_shared_from_this
allows you to work with an API which specifically acceptsshared_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. Jun 5, 2013 at 14:55 -
2
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;
};
-
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. Nov 28, 2017 at 23:47 -
1You 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 asNodePtr getParent const()
instead. If you absolutely need access to the internal raw pointer (best example: dealing with a C library), there'sstd::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. Dec 10, 2017 at 15:49