Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enable_shared_from_this - empty internal weak pointer?

Tags:

I'm using enable_shared_from_this<Base> and then inherit from Base. When trying to use shared_from_this() in Derived's constructor (not initializer list), I get an exception. Turns out that the internal weak pointer is null and doesn't point to this at all. How can this happen? My other use case of exactly this works perfectly fine. I don't even know where to start. I looked down at the source code of enable_shared_from_this, and it looks to me like that pointer would always be nullptr.

like image 865
Puppy Avatar asked Dec 20 '10 22:12

Puppy


People also ask

How enable_ shared_ from_ this works?

std::enable_shared_from_this is a standard solution that enables a shared_ptr managed object to acquire a shared_ptr to itself on demand. A class T that publicly inherits an std::enable_shared_from_this<T> encapsulates a std::weak_ptr<T> to itself that can be converted to a std::shared_ptr<T> when needed.

What is the use of Enable_shared_from_this?

std::enable_shared_from_this The class provides functionality that allows objects of derived classes to create instances of shared_ptr pointing to themselves and sharing ownership with existing shared_ptr objects.

Can weak pointer be copied?

A weak_ptr itself does not participate in the reference counting, and therefore, it cannot prevent the reference count from going to zero. However, you can use a weak_ptr to try to obtain a new copy of the shared_ptr with which it was initialized.

How can I get weak pointer from shared pointer?

You can make a weak pointer from a shared pointer, just using assignment = e.g. bool release_channel(std::shared_ptr<abstract::channel> ch) { std::weak_ptr<abstract::channel> weak_ch = ch; //... }


2 Answers

You cannot call shared_from_this() in the object's constructor. shared_from_this() requires that the object is owned by at least one shared_ptr. An object cannot be owned by a shared_ptr before it is constructed.

I would guess that the internal weak pointer is set when a shared_ptr takes ownership of the object for the first time. Before that point, there is no reference count struct that the weak pointer can reference.

like image 92
James McNellis Avatar answered Sep 19 '22 13:09

James McNellis


James McNellis's answer is right.

As for the explanation of the enable_shared_from_this template itself, which as you observe appears to do nothing, note 7 at the bottom of this page explains:

...the template enable_shared_from_this holds a weak_ptr object that points to the derived object. There's a chicken-and-egg problem, though, about how to initialize that weak_ptr object when there is no corresponding shared_ptr object. The implementation trick is that the constructors for shared_ptr know about enable_shared_from_this, and set the weak_ptr object during construction of a shared_ptr object that owns a resource that has enable_shared_from_this as a public base class.

like image 42
j_random_hacker Avatar answered Sep 18 '22 13:09

j_random_hacker