Say I have a juce::WeakReference<MyObject>
as suggested in the documentation - what happens if I try to access a pointer returned by the WeakReference
, if the original pointer is deleted by another thread in a multi-threaded situation?
MyObject* n = new MyObject();
WeakReference<MyObject> myObjectRef = n;
MyObject* pointer1 = myObjectRef; // returns a valid pointer to 'n'
// some other thread deletes n at this point
pointer1->doSomething(); // behaviour?
Is it even possible to safely use WeakReference
in a multi-threaded environment without locking, if there are no guarantees that the underlying pointer is still valid right after checking if it is?