Boost logo

Boost :

From: Dave Abrahams (abrahams_at_[hidden])
Date: 1999-08-01 12:58:33

> Although it seems impossible to create a non-intrusive weak
> pointer class that can track the liveness of an arbitrary class of
> object, it is possible to create one that tracks the liveness of
> objects controlled by shared_ptr.
> The changes to out current shared_ptr would be:
> Switch to the "indirect" implementation of shared_ptr. That
> is, the shared_ptr contains just a pointer to a handle struct
> containing a pointer to the object shared and the count.
> Add a second count field for the number of weak pointers using
> the object. When the number of shared pointers goes to zero
> the shared object is deleted and the pointer to object field is
> zeroed out, but the handle struct is not deleted until both
> counters go to zero.

Fiendishly clever!

1. How do you create a weak_ptr? I presume its constructor would need to
take a shared_ptr parameter...

2. This design, though not intrusive in the same sense, places other
restrictions on the object being pointed at: you shouldn't make a weak_ptr
to an object created on the stack.

3. In general, the intrusive "notify somebody when I'm destroyed" paradigm
is still quite useful. For example, you can use it to design a collaboration
system where a "broadcaster" transmits notifications to any number of
arbitrary "receivers". When a receiver is destroyed, it must be removed from
the broadcaster's list.


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