Questions tagged [enable-shared-from-this]
36 questions
34
votes
1 answer
Use of enable_shared_from_this with multiple inheritance
BI am using enable_shared_from_this in my code, and I am not sure if its usage is correct. This is the code:
class A: public std::enable_shared_from_this
{
public:
void foo1()
{
auto ptr = shared_from_this();
}
};
class…

Aarkan
- 3,811
- 6
- 40
- 54
32
votes
2 answers
Trivial cases of shared_ptr and weak_ptr failing
I'm having trouble using shared_ptr and weak_ptr along with enable_shared_from_this.
When I google the symptoms of what I'm seeing, everybody suggests "you cannot use shared_from_this() when there are no shared_ptr instances owning your object.
But…

LubosD
- 781
- 6
- 18
23
votes
4 answers
std::enable_shared_from_this; public vs private
I was playing around for a bit using the shared_ptr's and enable_shared_from_this, while I run into something I don't really understand.
In my first attempt I constructed something like this:
class shared_test :…

user2546926
- 385
- 3
- 12
22
votes
3 answers
When should we use std::enable_shared_from_this
I just knew std::enable_shared_from_this form this link.
But after reading the code below, I don't know when to use it.
try {
Good not_so_good;
std::shared_ptr gp1 = not_so_good.getptr();
} catch(std::bad_weak_ptr& e) {
…

Yves
- 11,597
- 17
- 83
- 180
14
votes
3 answers
boost shared_from_this and multiple inheritance
I am currently having some troubles when using boost enable_shared_from_this and multiple inheritance.
The scenario can be described as follows:
Class A implements some functionality and should inherit from enable_shared_from_this
Class B…

ChrisPeterson
- 419
- 4
- 15
10
votes
3 answers
Why does enable_shared_from_this embed a weak pointer instead of embedding the reference counter directly?
The enable_shared_from_this helper contains a weak pointer that is set when creating shared pointer to the object. That means there is the reference-count (allocated separately or together with the object using make_shared) and an extra weak_ptr in…

Jan Hudec
- 73,652
- 13
- 125
- 172
10
votes
3 answers
Double inheritance of enable_shared_from_this
I have an object (Z) which derives from two other objects (A and B).
A and B both derive from enable_shared_from_this<>, respectively enable_shared_from_this and enable_shared_from_this.
Of course I call shared_from_this() on Z. And of course…

Offirmo
- 18,962
- 12
- 76
- 97
8
votes
3 answers
Must enable_shared_from_this be the first base class?
My class inherits from multiple bases, one of which is std::enable_shared_from_this. Must it be the first base?
Suppose the following example code:
struct A { ~A(); };
struct B { ~B(); };
struct C : A, B, std::enable_shared_from_this…

Filipp
- 1,843
- 12
- 26
7
votes
2 answers
two shared_ptr from same enable_shared_from_this instance
Given this class which is enable_shared_from_this:
class connection : public std::enable_shared_from_this
{
//...
};
Suppose I create two instances of std::shared_ptr from the same connection* as follows:
std::shared_ptr…

Nawaz
- 353,942
- 115
- 666
- 851
7
votes
2 answers
bad weak pointer when base and derived class both inherit from boost::enable_shared_from_this
I have a base class which derives from boost::enable_shared_from_this, and then another class which derives from both the base class and boost::enable_shared_from_this:
#include
#include…

Ken
- 619
- 1
- 7
- 11
6
votes
2 answers
terminate called after throwing an instance of 'std::bad_weak_ptr' what(): bad_weak_ptr?
I'm learning smart pointers and shared_from_this. In Class Inheritance Relations, it will be very hard to understand.
I have two base classes CA and CB, they are derived from enable_shared_from_this, and the child class CC derives from both CA and…

justin.shen
- 61
- 1
- 5
5
votes
2 answers
How can I use std::enable_shared_from_this in both super and subclass?
I have two classes A and B, where B is a subclass of A. I need both classes to use std::enable_shared_from_this.
I tried this:
#include
#include
#include
class A : public std::enable_shared_from_this {
public:
…

oz1cz
- 5,504
- 6
- 38
- 58
3
votes
1 answer
When is enable_shared_from_this useful?
I have been trying to understand the usefulness of inheriting from enable_shared_from_this although the working of this mechanism was somewhat explained there. I couldn't find the answer to this question on that post so I am posting it here.
In one…

Rajeshwar
- 11,179
- 26
- 86
- 158
3
votes
2 answers
Why does enable_shared_from_this crash if inheritance is not public instead of erroring out
I was using shared_ptr in a project. And at one point I had to store the raw pointers as a void then later convert it back to its shared_ptr form in a callback where the void* was passed. But for some reason the code kept crashing. I didn't…

Irelia
- 3,407
- 2
- 10
- 31
3
votes
2 answers
Could shared_from_this be implemented without enable_shared_from_this?
There seem to be some edge-cases when using enabled_shared_from_this. For example:
boost shared_from_this and multiple inheritance
Could shared_from_this be implemented without using enable_shared_from_this? If so, could it be made as fast?

Taylor
- 5,871
- 2
- 30
- 64