Do you recommend to use SafePointer or std::unique_ptr?

Hi JUCE masterminds,

I’ve mixed using SafePointer and std::unique_ptr in my project. They both seem to work fine. But I’d like to be more careful about what their best use case should be, which I don’t have much experience differentiating the two. Any idea?


They have a different behaviour and therefore a different use:

std::unique_ptr is an owning pointer, i.e. it will delete the pointee object once the unique_ptr goes out of scope.

SafePointer is an observing pointer. It doesn’t do anything in terms of lifetime, but if the pointee is deleted somewhere else, it makes sure the SafePointer is set to point to nullptr, instead to the freed memory.

Thank you for the answer. I’m clear on what to do :grinning: