Boost logo

Boost :

From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2002-04-19 16:42:34


"Darin Adler" <darin_at_[hidden]> wrote in message
news:DD493CCF-53D9-11D6-9E79-0003935B80A2_at_bentspoon.com...
>
> On Friday, April 19, 2002, at 01:42 PM, Peter Dimov wrote:
>
> > From: "Fernando Cacciola" <fcacciola_at_[hidden]>
> >> int main()
> >> {
> >> boost::intrusive_ptr<X> px(new X); // obj's internal count=1
> >> boost::intrusive_ptr<X> px2(px); // obj's internal count=2
> >> boost::shared_ptr<X> px3(px2); // obj's internal count still =2,
> >> detached count=1
> >>
> >> px = 0; // obj's internal count=1
> >> px2 = 0; // obj's internal count=0 -----> obj suicides itself....
> >>
> >> *px3 ; // undefined behavior, pointee no longer alive.
> >> }
> >>
> >> What I am missing?
> >
> > Try it. :-)
>
> What Peter means is:
>
> 1) If X is publicly derived from shared_count, it will just work,
> because shared_ptr will find the shared_count part and use it for
> reference counting. There won't be a "detached count".
>
> 2) If X is not publicly derived from shared_count, then the
> shared_ptr constructor for px3 will fail to compile.
>
> Now you should try it and see that it works as designed.
>
Yes, I figured this out once I realized that there must have been a change
in shared_ptr as well, and found it in the CVS.

The new shared_ptr works pretty much as I originally suggested when I
presented shared_object as a base class (a role played now by shared_count);
that is, it recognizes objects capable of having their own counts. The main
difference with my idea and Peter's design, is that in his code, is
intrusive_ptr<> which communicates to the shared_ptr constructor not to use
the detached count.

I wonder if this could be confusing: shared_ptr uses a detached or intrusive
count depending on how it is constructed.... I find more intuitive to expect
that shared_ptr uses a detached or intrusive count depending on the pointee
type, regardless of how you create it.

So, I'm starting to think that Dietmar is right about having only
shared_ptr<> (instead of both shared_ptr<> and intrusive_ptr<>).

--
Fernando Cacciola
Sierra s.r.l.
fcacciola_at_[hidden]
www.gosierra.com

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