Boost logo

Boost Users :

Subject: Re: [Boost-users] Why cannot asio tcp socket server detect TCP disconnection when the client device lost power?
From: Leon Mlakar (leon_at_[hidden])
Date: 2017-06-30 00:31:49


> For monitoring your session you should use map/any_container with
> weak_ptrs, not shared_ptrs.
>
>
> Interesting, I haven't thought that, what will be the issues and
> consequences to use shared_ptrs not weak_ptrs? Thanks for your advice.
Shared pointers keep objects in existence, weak pointers don't. At a
price of an additional lock() operation, of course (which, except in
really time critical applications, should not be an issue).

It's never a good idea to have too many shared pointers around,
especially not in the monitoring maps as they may keep objects alive
longer than intended.

Additional advantage of using weak pointer in monitoring maps is that if
the lock() fails it's okay to remove the entry from the map as the
object is no longer there.

Now, for the sake of being thorough - although weak pointer will not
keep the object in existence, it will keep it's shared count alive.
Which, in the case of using std::make_shared to allocate shared count
and object memory in a single block, will keep the entire block of
memory allocated until the last weak pointer is gone ... hence even in
the case of using weak pointers it is a good idea to do occasional
cleanup, which is rather trivial via failed lock().

Cheers,
Leon
>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> https://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