Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2002-10-01 09:43:11

From: "Peter Dimov" <pdimov_at_[hidden]>

> If you test the weak_ptr, you'll see that it will properly report that
> expired(); use_count() will be zero; and if you try to construct a
> shared_ptr from it, the construction will fail.
> > But when I am using weak_ptr with intrusive_ptr it know works like
> > pointer?
> >
> > I think I missed something. Is it bug or feature?
> It is a "feature". The reference count is contained in counted_base, and
> it's embedded into the object. It is not possible to destroy the object
> without also destroying the counted_base, and this cannot happen while
> are still weak pointers to that counted_base. That's why
> counted_base::dispose() is invoked when the last shared_ptr is destroyed,
> let you release the resources contained in your object.

I don't know how long I'll be able to hold this position in the face of
move semantics, but so far I've been successful at promoting the idea that
a "destructible but otherwise unusable" state is misguided for most
classes. One thing I don't like about this arrangement is that is tends to
encourage that sort of design. I think it might be avoidable by having the
user inherit from something like this:

struct intrusive_count
    intrusive_count() : count(new counted_base) {}
    ~intrusive_count() { if (count->weak_count == 0) delete count; }
    counted_base* count;

Just a thought.

           David Abrahams * Boost Consulting
dave_at_[hidden] *

Boost list run by bdawes at, gregod at, cpdaniel at, john at