Boost logo

Boost Users :

Subject: Re: [Boost-users] [smart_ptr] weak_ptr pointer semantics
From: Tim Musson (Tim.Musson_at_[hidden])
Date: 2011-11-11 12:18:45


After re-reading your reply, I think your confusion comes from the
following idea: "I still have weak_ptr<> which has a counter ". A line
from the weat_ptr<> documentation states that "When the last
*shared_ptr* to the object goes away and the object is deleted, the
attempt to obtain a *shared_ptr* from the *weak_ptr* instances that
refer to the deleted object will fail". So, weak_ptr does not have a
counter, that's it's weakness ;) I assume that clears things up for you.
Since I had already typed a longer response walking through my mental
journey, I'll leave it attached below in case it's of use to anyone else...

-Tim

Honestly, I'm pretty new to Boost and forgot about get_pointer() :)
After being reminded of it, I originally agreed that if you're going to
break reference counting at that level, you might as well break it
everywhere. What started confusing me is why such an easy mechanism was
made to bypass reference counting in the first place. I read
http://www.boost.org/doc/libs/1_47_0/libs/bind/mem_fn.html and while I
don't really understand the need for certain utility functions to get a
raw pointer from a smart pointer, they obviously wanted a common
interface for retrieving a raw pointer from any smart pointer class,
even non-Boost ones. So, what makes sense to me now is that raw pointers
should not be used willy-nilly. They should only be used in utility
functions that really need them, and the intent is that *the raw pointer
must be scoped by the use of a smart, reference counted, pointer*. If
that's true (and I hope someone can verify), then it makes sense that
weak_ptr would not be allowed to return a raw pointer (because weak_ptr
is not reference counted). It seems to me, though, that get_pointer() is
too exposed and thus easily abused. It would be nice to at least see the
documentation beefed up to better clarify its intended usage and dangers.

On 11/10/2011 7:46 PM, Szymon Gatner wrote:
>> Because it is not safe to have a pointer to something and not reference
>> count it.
> Not sure what you mean here, I still have weak_ptr<> which has a counter
> and there is a shared_ptr<> (or not) somewhere out there. So counter exists
> as long as I still have any shared_ or weak_ pointer.
>
> See the explanation at
>> http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/weak_ptr.htm for a more
>> detailed argument of the same.
> Thanks, no idea how I missed that. Makes perfect sense in threaded world
> but not convinced for single-threaded.
>
> If you don't want safety in your pointers,
>> then use raw pointers. If you want safety, then you have to accept the
>> consequences...
> So using get() on shared/scoped/auto/unique_ptr somehow keeps me safe
> having raw pointer that bypasses all protection they provide but weak_ptr
> is too much? Not buying it ;)
>
> Cheers,
> Simon
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net