@demofox weak_ptr can 'let go' (as opposed to have shared_ptr in two places and having to coordinate) but I've rarely seen it talked about in any sort of tutorial on smart pointers
@mmby oh yeah, weak pointers are pretty nice. They were used heavily in the lithtech engine at monolith for networked game objects and it was a real positive experience. The lifetime of these things were real complex, but the weak pointers made that complexity largely invisible.
@demofox@mmby today I wouldn't consider anything else but generation-counted-index-handles. Gives you weak references with dangling protection, and also encourages to properly separate system internals from the outside world.
Raw pointers are fine inside the system, but only pass handles across system boundaries.
@floooh@demofox@mmby This. I'm always surprised how "new" or alien of a concept this is to even senior programmers.
I blame the ubiquity of standard C++ in our industry for this. Your blog post on handles has come in handy a great many times when explaining the concept 🙂.
There’s been some push back to handles at work actually. The problem is that they make the program harder to debug. With pointers you can easily see the value in the debugger, with a handle just an index somewhere else.
The current compromise is to store a pointer the handle, but only in debug builds. Not sure if I like it.
@PetorSFZ@demofox@mmby yeah I can see the advantages of carrying a debug-pointer with the handles, but it requires to make the underlying struct public too (which ideally is private to the system that created the handle).
But in general I think "fat handles" that carry additional information in debug mode aren't a bad idea per se.
@demofox definitely! and having a smart pointer usage/system without an easy way to track and display reference holders is just asking for long, very frustrating memory usage debugging sessions. :)
Add comment