Chuck Messenger wrote: > 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.
Why are you using a shared_ptr<Y> in X? A Y* or a weak_ptr<Y> seems more appropriate for parent links. > 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." I wouldn't call it "perfectly sound". A pimpl_->x.y.reset() will leave your pimpl_ dangling and ~pimpl_ will crash. > Why do I want to do all this? Well, I could get into the very sound > reasons if anyone is interested. Yes, please do. > It is a technique for avoiding circular shared_ptr references. What you've shown so far looks like a technique for creating circular references. ;-) _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost