Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2005-06-16 08:10:32


Larry Evans wrote:
> But if the problematic roots are known in advance, why doesn't the
> programmer simply use weak_ptr's to them? I guess it's because
> the programmer doesn't know he's connecting to a problematic root,
> although he knows what they are.

The general scenario is that you have a class X that contains a
shared_ptr<Y>, where the Y's can also contain shared_ptr<Y>s to each other,
but the user never sees an Y directly.

You know that all your roots are in the instances of X, but you can't use
weak_ptr to link the Y's because this won't keep a cyclic structure alive
until the last X to it is gone.

However you can reset_and_collect the shared_ptr<Y> in ~X. (The problem that
prevented Eric from using it is that reset_and_collect may throw.)
Alternatively, you can also keep a {vector, set}< weak_ptr<Y> > global root
structure, add to it in X::X and scan and prune it periodically.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk