Boost logo

Boost :

From: Chuck Messenger (chuckm_at_[hidden])
Date: 2003-05-27 06:41:15


Suppose you have this:

     struct X {
         shared_ptr<Y> y;
         ...
     };

     struct Y {
         X x;
         ....
     };

     struct Z {
         Z() : pimpl_(new Y()) { pimpl_->x.y = pimpl_; }
         shared_ptr<Y> pimpl_;
         ...
     };

I'm using the "pimpl" idiom. When I create Z, I create an embedded Y
("y"), setting y's x so that it points to y.

The problem is that Y will now never die. What I'd like to do is to
decrement the reference count in x, when I construct the Y:

         Z() : pimpl_(new Y()) { pimpl_->x.y = pimpl_;
                   pimpl_.decrement_reference_count(); }

This is perfectly sound -- it decrements the reference count from 2 to
1. It says, basically, "the mother structure contains a self-referring
pointer. If that's the only one left, then kill the mother structure."

Why do I want to do all this? Well, I could get into the very sound
reasons if anyone is interested. It is a technique for avoiding circular
shared_ptr references.

     - Chuck Messenger


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