Boost logo

Boost Users :

From: Niklas Wiberg (nwlists-boostusers_at_[hidden])
Date: 2005-01-17 05:00:27

I have the following situation:
In the constructor body of class A, I need to call functions that take a
shared_ptr<A> as a parameter. These functions do not store the
shared_ptr<A>, but in some situations a weak_ptr<A>.
There is not yet any shared_ptr owning the A instance, since this is
during A construction...

At first, I thought using a shared_ptr<A> tmpThis(this, NullDeleter())
was a good idea.
But when I later on (when another shared_ptr actually owns the A
instance) want to use shared_from_this on A, I get an exception since
the use_count of the internal weak_ptr is based on the first temporary
shared_ptr that I used during construction (with the NullDeleter).

Am I correct in my assumption that the internal weak_ptr in
enable_shared_from_this is initialized by the first shared_ptr and that
will prevent any later use of shared_from_this when I have another
shared_ptr (with a default deleter) owning the element?

I guess that a static create function would solve the problem, but
changing that would be quite a big undertaking in this particular case
so I thought I'd check if someone knows a simple solution to this...

I added a small example code snipped below...

Best regards,


void foo(shared_ptr<A> a);
void otherFunction(shared_ptr<A> a);

class A : public enable_shared_from_this<A> {
   A() {
     // Can't use shared_from_this since no shared_ptr owns this yet
     shared_ptr<A> thisTmp(this, NullDeleter());

   bar() {
     otherFunction(shared_from_this()); // Throws exception...

int main() {
   shared_ptr<A> a(new A());

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at