0

I am trying to learn a bit about std::weak_ptr(semantics and usecases), however I cannot find many resources online. Cppreference didn't help at much, and there are a few other sites, but they didn't really help either.

Thank you in advance!

H-005
  • 467
  • 3
  • 15
  • 2
    What don't you understand about them? – Alan Birtles Jul 31 '20 at 05:45
  • @AlanBirtles Basically nothing. I know that the object a `weak_ptr` points to is destroyed when there are no `strong_ptr` to point at it, and that's pretty much it. I can't really find any good examples on it, when it is used and it's syntax (how to check if the value it points to still exists, etc) – H-005 Jul 31 '20 at 05:52
  • That's all there is to know? – Alan Birtles Jul 31 '20 at 06:03
  • 2
    Maybe it could deserve a true answer, but already closed. `weak_ptr` are mainly used in observers or to break cyclic dependencies. An observer wants to be able to print the state of an object *if it is still used elsewhere*, and just know that it is no longer used. A weak_ptr allows the object to be destroyed. `lock` extract a `shared_ptr` from it if the object still exists, or return a default `shared_ptr` (actually pointing to NULL) if the object is expired. [cppreference std::weak_ptr.lock](https://en.cppreference.com/w/cpp/memory/weak_ptr/lock) contains a code example... – Serge Ballesta Jul 31 '20 at 06:20
  • 1
    ... another possible use case is breaking reference cycles. If A has a shared pointer to B, B to C and C to A, none of the objects will ever be destroyed because each of them has an owner. If one of the pointers is made weak, when the objects are no longer needed, the one pointed to by the weak_ptr will be destroyed, and from there the whole chain. Simply you must get a temporary `shared_ptr` from the `weak_ptr` with `lock` before using it, just like an observer would do. – Serge Ballesta Jul 31 '20 at 06:28
  • @SergeBallesta hmm... that's an interesting use of `weak_ptr`. Will note it for future. Though, I suppose you already deal with fixing some crappy design when you use `weak_ptr` to break cyclic dependencies. My sympathies. – ALX23z Jul 31 '20 at 12:36
  • @SergeBallesta Thank you, this made things clearer – H-005 Jul 31 '20 at 16:27
  • @ALX23z: never had to deal with cyclic dependencies in C++. But I remember problems in a Java web app with cyclic dependencies on Spring beans. And as it was nicely documented in Spring doc, I assume that it was not that uncommon. – Serge Ballesta Jul 31 '20 at 16:46
  • @SergeBallesta hmm... I can see Cyclic dependencies occurring in Jave/C# where pointers by nature are "smart". In C++ one normally builds hierarchical dependency with unique_ptr/shared_ptr one way and raw pointers the other - so no cycles to begin with. I'd very unhappy to see cycling dependency between classes in C++. – ALX23z Jul 31 '20 at 17:27

1 Answers1

2

It is a non-owning reference to a shared_ptr that can be safely upgraded to the shared_ptr. weak_ptr isn't frequently used. Never had to use it thus far. Though, it is partially because I usually shy away from shared_ptr unless needed.

weak_ptr can be used to manage lifetime of a shared_ptr - which can be very messy. For instance, if a bunch of class instances have a copy of the shared_ptr how do you delete it when you need to? Go over every single instance that leaked it who-knows-where?

Now instead you can hold a single shared_ptr and supply only weak_ptr and require that they upgrade to shared_ptr only during active use. Once you need it deleted just clear the shared_ptr and it will delete itself after its active duty is over.

ALX23z
  • 4,456
  • 1
  • 11
  • 18