Developer Notes

This isnt any safer. The IsValid is check will fail or succeed in the same way each time

Description

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.

Steps to Reproduce
  1. Open the UE4.sln file in Visual Studio.
  2. Open the SViewport.cpp file.
  3. Locate the SViewport::OnMouseLeave function.

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.

Have Comments or More Details?

Head over to the existing Questions & Answers thread and let us know what's up.

0
Login to Vote

Won't Fix
ComponentTools
Affects Versions4.6.14.8.34.10
CreatedAug 31, 2015
ResolvedSep 1, 2015
UpdatedJul 14, 2021