|
Boost : |
From: Beman Dawes (bdawes_at_[hidden])
Date: 2001-03-19 07:34:40
At 09:50 PM 3/18/2001, Joe Gottman wrote:
>From: "Beman Dawes" <bdawes_at_[hidden]>
>> >1. In share(), shouldn't the code "if (pn != rpn)" be "if (px !=
>> >rpx)"?
>>
>> They are equivalent. Something is disastrously wrong if (pn != rpn) ==
>(px
>> != rpx). Think about it. If the two object pointers point to the same
>> object, the two count pointer had better point to the same count. I'll
>add
>> a comment to the code mentioning that. Similar in shared_array. I
guess
>> we could assert ((pn==rpn) == (px==rpx)), but it doesn't seem worth it
to
>> me.
>
>px != rpx isn't quite equivalent to pn != rpn. Consider the following
code:
>
>shared_ptr<int> p1, p2; // p1 and p2 are both initialized with px == 0
>p1 = p2; // px 's are equal, pn's are not. If share were to compare px's
we
>would have a memory leak in the pn's.
You're right! Thanks! I've changed the comment to read:
if (pn != rpn) { // Q: why not px != rpx? A: fails when both == 0
Greg in private email also pointed out a tricky case of when the pointers
aren't the same type but are related by multiple inheritance to point to
different places in the object. I'm going to have to think about that one.
--Beman
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk