Crash occured when invoke AddToRoot and AddToViewport at one time

code to reproduce issue:

UUserWidget* Widget = CreateWidget<UUserWidget>(MyPlayerController, WidgetTemplate);
if (Widget)
{
	Widget->AddToRoot();
	Widget->AddToViewport();
}

If invoke AddToRoot and AddToViewport at the same time, UE4Editor would crash when click Stop button
of Editor at runtime.

crash dump:

Assertion failed: !IsRooted() [File:d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\coreuobject\public\uobject\UObjectBaseUtility.h] [Line: 135] 



UE4Editor_Core!FDebug::AssertFailed() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\core\private\misc\outputdevice.cpp:421]
UE4Editor_UnrealEd!<lambda_26419e543909ee92ebfb672b1e9c08dc>::operator()() [d:\build\++ue4+release-4.13+compile\sync\engine\source\editor\unrealed\private\playlevel.cpp:384]
UE4Editor_CoreUObject!ForEachObjectWithOuter() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\coreuobject\private\uobject\uobjecthash.cpp:678]
UE4Editor_UnrealEd!UEditorEngine::EndPlayMap() [d:\build\++ue4+release-4.13+compile\sync\engine\source\editor\unrealed\private\playlevel.cpp:380]
UE4Editor_UnrealEd!UEditorEngine::Tick() [d:\build\++ue4+release-4.13+compile\sync\engine\source\editor\unrealed\private\editorengine.cpp:1653]
UE4Editor_UnrealEd!UUnrealEdEngine::Tick() [d:\build\++ue4+release-4.13+compile\sync\engine\source\editor\unrealed\private\unrealedengine.cpp:371]
UE4Editor!FEngineLoop::Tick() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\launch\private\launchengineloop.cpp:2834]
UE4Editor!GuardedMain() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\launch\private\launch.cpp:156]
UE4Editor!GuardedMainWrapper() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\launch\private\windows\launchwindows.cpp:126]
UE4Editor!WinMain() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\launch\private\windows\launchwindows.cpp:202]
UE4Editor!__scrt_common_main_seh() [f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:264]
kernel32
ntdll

I think UE4 should prevent users to invoke AddToRoot and AddToViewport at the same time, or give an exact tip to warn users from that usage.

Hello,

Where are you adding/calling this code? Is this happening inside an actor class, or in a begin play function, or a function being called later during runtime. Please include additional details to help me reproduce / investigate the behavior you’re seeing.

Hello, Doug Wilson. I call it in a Widget Button click event in a custom UUserWidget class. and I have several UUserWidget* need to create in my app.

The reason the editor is crashing on exit is because “Widget” is not being cleaned up when the object it is being added to is destroyed. Adding a call to Widget->RemoveFromRoot(); at any point before the object is destroyed should prevent the editor from crashing on exit.

Cheers

Doug Wilson

Oh, I see, it’s my wrong. Ty so much!

Execuse me, I have another question, how can I get the event of Stop button of UE4Editor, this crash happens before the BeginDestroy of GameMode and UserWidget.

Oh, I see, Event of Stop button of Editor is MyGameInstance::Shutdown(), not in GameMode.

Spent a few months working on a project in C++ that contained quite a few widgets. I tried to reopen the editor recently and had this exact same crash when trying to use the stop button within the editor.

I was using the ‘AddToRoot()’ function to keep my widgets from being garbage collected. This worked fine for awhile but later proved very problematic for obvious reasons.

My solution was storing any pointers to widgets within an array containing UPROPERTY() on it, rather than rooting everything. (something I saw Rama suggest somewhere,ThankYou this works nicely!) I just wanted to share my experience in case anybody ever has this same problem. This post helped me out a lot and saved a few months of effort.

Editor 4.15.3


Visual Studio 2017