Boost logo

Boost :

From: Darin Adler (darin_at_[hidden])
Date: 2002-04-19 16:10:23


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.

     -- Darin


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