Description

RemoveAfterSecondsLambda in AddTimedDisplay function may access released memory during level transitions.
The following workaround works :

void UViewportStatsSubsystem::AddTimedDisplay(FText Text, FLinearColor Color, float Duration, const FVector2D& DisplayOffset /* = FVector2D::ZeroVector */)
{
...
		auto RemoveAfterSecondsLambda = [](TWeakPtr<FUniqueDisplayData> DisplayItem, TArray<TSharedPtr<FUniqueDisplayData>>* ConditionArray)
		{
			if (DisplayItem.IsValid() && ConditionArray)
			{
				ConditionArray->Remove(DisplayItem.Pin());
			}
		};

		TimerDel.BindLambda(RemoveAfterSecondsLambda, TWeakPtr<FUniqueDisplayData>(Message), &UniqueDisplayMessages);
		MyWorld->GetTimerManager().SetTimer(TimerHandle, TimerDel, Duration, /* bInLoop= */ false);

Steps to Reproduce
  1. Open the attached project on ue5.0 [Link Removed]
  2. Start PIE

Result:

Access to released memory occurs and crashes

 

Callstack

UnrealEditor-Engine-Win64-Debug.dll!TArray<TSharedPtr<UViewportStatsSubsystem::FUniqueDisplayData,1>,TSizedDefaultAllocator<32>>::RemoveAll<<lambda_fc5ce13b0f7bf91be480530184f9f9f7>>(const TArray<TSharedPtr<UViewportStatsSubsystem::FUniqueDisplayData,1>,TSizedDefaultAllocator<32>>::Remove::__l2::<lambda_fc5ce13b0f7bf91be480530184f9f9f7> & Predicate) Line 2414    C++
UnrealEditor-Engine-Win64-Debug.dll!TArray<TSharedPtr<UViewportStatsSubsystem::FUniqueDisplayData,1>,TSizedDefaultAllocator<32>>::Remove(const TSharedPtr<UViewportStatsSubsystem::FUniqueDisplayData,1> & Item) Line 2393    C++
UnrealEditor-Engine-Win64-Debug.dll!UE::Core::Private::Tuple::TTupleBase<TIntegerSequence<unsigned int,0,1>,TSharedPtr<UViewportStatsSubsystem::FUniqueDisplayData,1>,TArray<TSharedPtr<UViewportStatsSubsystem::FUniqueDisplayData,1>,TSizedDefaultAllocator<32>> *>::ApplyAfter<<lambda_6685a56b7cd5c2e957edcd333da7f058> &>(UViewportStatsSubsystem::AddTimedDisplay::__l8::<lambda_6685a56b7cd5c2e957edcd333da7f058> & Func) Line 324    C++
UnrealEditor-Engine-Win64-Debug.dll!TBaseFunctorDelegateInstance<void __cdecl(void),FDefaultDelegateUserPolicy,<lambda_6685a56b7cd5c2e957edcd333da7f058>,TSharedPtr<UViewportStatsSubsystem::FUniqueDisplayData,1>,TArray<TSharedPtr<UViewportStatsSubsystem::FUniqueDisplayData,1>,TSizedDefaultAllocator<32>> *>::Execute() Line 831    C++
UnrealEditor-Engine-Win64-Debug.dll!FTimerUnifiedDelegate::Execute() Line 51    C++
UnrealEditor-Engine-Win64-Debug.dll!FTimerManager::Tick(float DeltaTime) Line 925    C++
UnrealEditor-Engine-Win64-Debug.dll!UWorld::Tick(ELevelTick TickType, float DeltaSeconds) Line 1597    C++
UnrealEditor-UnrealEd-Win64-Debug.dll!UEditorEngine::Tick(float DeltaSeconds, bool bIdleMode) Line 1777    C++
UnrealEditor-UnrealEd-Win64-Debug.dll!UUnrealEdEngine::Tick(float DeltaSeconds, bool bIdleMode) Line 474    C++
UnrealEditor-Win64-Debug.exe!FEngineLoop::Tick() Line 5215    C++
UnrealEditor-Win64-Debug.exe!EngineTick() Line 63    C++
UnrealEditor-Win64-Debug.exe!GuardedMain(const wchar_t * CmdLine) Line 186    C++
UnrealEditor-Win64-Debug.exe!LaunchWindowsStartup(HINSTANCE__ * hInInstance, HINSTANCE__ * hPrevInstance, char * __formal, int nCmdShow, const wchar_t * CmdLine) Line 272    C++
UnrealEditor-Win64-Debug.exe!WinMain(HINSTANCE__ * hInInstance, HINSTANCE__ * hPrevInstance, char * pCmdLine, int nCmdShow) Line 330    C++
[External Code]    

Have Comments or More Details?

There's no existing public thread on this issue, so head over to Questions & Answers just mention UE-172400 in the post.

0
Login to Vote

Fixed
ComponentUE - Gameplay
Affects Versions5.05.1
Target Fix5.3
Fix Commit23976049
Main Commit23976049
CreatedDec 8, 2022
ResolvedFeb 2, 2023
UpdatedMay 10, 2023
View Jira Issue