Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Weak pointers to objects not managed by a shared_ptr

53 views
Skip to first unread message

wthol...@gmail.com

unread,
Jan 15, 2015, 8:46:05 PM1/15/15
to
http://www.boost.org/doc/libs/1_57_0/libs/smart_ptr/sp_techniques.html#weak_without_shared

Is this as dangerous as it looks? After you lock the weak_ptr to use the object, the object could be destructed and you're left with a dangling shared_ptr.

- Taylor

Öö Tiib

unread,
Jan 16, 2015, 9:21:51 AM1/16/15
to
On Friday, 16 January 2015 03:46:05 UTC+2, wthol...@gmail.com wrote:
> http://www.boost.org/doc/libs/1_57_0/libs/smart_ptr/sp_techniques.html#weak_without_shared
>
> Is this as dangerous as it looks?

No.

> After you lock the weak_ptr to use the object,
> the object could be destructed and you're left with a dangling shared_ptr.

You can check in destructor of X that 'this_.use_count() == 1' if you feel worried.

When it is not then there is apparent logic defect somewhere and so X may blow
up the application with message suggesting to fix the defect ASAP.

wthol...@gmail.com

unread,
Jan 16, 2015, 12:07:27 PM1/16/15
to
Good point! Thanks!
0 new messages