This isnt any safer. The IsValid is check will fail or succeed in the same way each time
A user reported a one-time crash that possibly occurred when a TWeakPtr became invalid immediately after passing an IsValid check, before the TWeakPtr could be used. The SViewport.cpp file contains several instances of code that uses the following pattern:
if (ViewportInterface.IsValid())
{
ViewportInterface.Pin()->OnMouseLeave(MouseEvent);
}
The user suggested that this code may be better written as follows:
TSharedPtr<ISlateViewport> PinnedInterface = ViewportInterface.Pin();
if (PinnedInterface.IsValid())
{
PinnedInterface->OnMouseLeave(MouseEvent);
}
Please investigate whether using Pin prior to IsValid in these cases would be better able to prevent a potential race condition.
Note: I was unable to repro the actual crash that the user experienced, and they apparently only saw it happen a single time.
RESULT:
The code in the first code snippet shown in the Description is visible.
EXPECTED:
The code in the second code snippet shown in the Description may be safer.
Head over to the existing Questions & Answers thread and let us know what's up.