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@lists.boost.org
https://lists.boost.org/mailman/listinfo.cgi/boost-users