From reading the docs, it says that “If you’re using a component which may be deleted by another event that’s outside of your control, use a SafePointer instead of a normal pointer to refer to it, and you can test whether it’s null before using it to see if something has deleted it.”
So I’m just wondering, based on examples of its usage in JUCE Source, why it is sometimes tested and sometimes not?
As an example, here is some code from the JUCE AudioPluginHost, that launches the AudioSettings dialog:
auto safeThis = SafePointer<MainHostWindow> (this);
w->enterModalState (true,
ModalCallbackFunction::create
([safeThis] (int)
{
auto audioState = safeThis->deviceManager.createStateXml();
getAppProperties().getUserSettings()->setValue ("audioDeviceState", audioState.get());
getAppProperties().getUserSettings()->saveIfNeeded();
if (safeThis->graphHolder != nullptr)
if (safeThis->graphHolder->graph != nullptr)
safeThis->graphHolder->graph->graph.removeIllegalConnections();
}), true);
From my reading, the first line (in the callback) would crash if safeThis was nullptr, wouldn’t it? Then, safePointer is tested for null later on, but not those first 3 lines.
Wondering what best practice is for this…