
ethann.castell@caliton.com wrote:
I've manage to narrow down what the problem is and created a test case that simulates my real code.
The problem occurs in a destructor (of B1) when it tries to get a shared pointer to shared_from_this() from its base class (base_class). I think that the execution of the destructor hierarchy has deleted the original shared_ptr to B2 meaning that the shared_from_this() returns an error (because shared_from_this() requires that the object is managed by a shared_ptr).
My original destructor code was using raw pointers (ie. this) which worked fine because the base_class destructor was last to be called and was still alive at the time when ~B2() is called. This seems like it could be a limitation of boost::shared_ptr?
~B2 is executed after the last shared_ptr instance to the object has been destroyed. There is no way to revive the object once its destruction has been started, so it doesn't make sense to allow a shared_ptr to the object to be created; it won't be able to fulfill its promise to keep the object alive. You can create a shared_ptr in ~B2 with a null deleter, but its limitations remain. If you pass it to outside code that stores it for later use, things will break sooner or later. In general, functions that don't need to control the lifetime of the object won't need to take a shared_ptr, just a reference (or a raw pointer); it isn't clear why this is not the case in your code.