I try to understand the behavior of std::enable_shared_from_this class, but I cant understand it. So I've write a simple program to test different situations.
Can someone explain me the behavior of the following code, because I can't explain the observed result.
Thanks for you help.
#include <iostream>
#include <memory>
struct C : std::enable_shared_from_this<C> { };
int main () {
{//test 1
std::shared_ptr<C> foo, bar;
foo = std::make_shared<C>();
bar = foo->shared_from_this(); //ok
std::cout<<"shared_ptr : ok"<<std::endl;
}
{//test 2
std::shared_ptr<C> foo = std::shared_ptr<C>(new C);
std::shared_ptr<C> bar;
bar = foo->shared_from_this(); //ok
std::cout<<"shared_ptr + New : ok"<<std::endl;
}
{//test 3
C* foo = new C;
std::shared_ptr<C> bar;
bar = foo->shared_from_this(); //throw std::bad_weak_ptr
std::cout<<"New : ok"<<std::endl;
}
{//test 4 (should make a invalid free of something like that)
C foo;
std::shared_ptr<C> bar;
bar = foo.shared_from_this();//throw std::bad_weak_ptr
std::cout<<"local : ok"<<std::endl;
}
return 0;
}
And here is the output:
shared_ptr : ok
shared_ptr + New : ok
terminate called after throwing an instance of 'std::bad_weak_ptr'
what(): bad_weak_ptr
The contract for enable_shared_from_this
is that it will provide a shared_ptr
when you call shared_from_this()
if the object is managed by a shared_ptr
. It cannot create a shared_ptr
by itself if it is not already being managed.
You already figured out in the last test case why it would be a bad idea to allow shared_from_this()
to create a shared pointer for you...
If I remember correctly from the original boost documentation, "There must exist at least one shared_ptr instance p that owns t". For cases 3 and 4 there isn't at the point you call shared_from_this().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With