Description

A user reported that UToolMenus::GenerateWidget will crash if UToolMenus::AddReferencedObject is called during the construction of a menu. Using the above in-code repro steps, I can repro this in a unit test.

Steps to Reproduce

I do not know of a repro in the editor but with this code

UToolMenu* ToolMenu = UToolMenus::Get()->RegisterMenu("MyMenu");

ToolMenu->AddDynamicSection("MyDynamicLegacySection", FNewToolMenuDelegateLegacy::CreateLambda([](FMenuBuilder& InBuilder, UToolMenu* InMenu)
{
    TArray<UObject*> Array;
    FReferenceFinder Finder(Array);
    UToolMenus::AddReferencedObjects(UToolMenus::Get(), Finder);
}));

UToolMenus::Get()->GenerateWidget("MyMenu", FToolMenuContext());

you will get a crash in UToolMenus::GenerateWidget(UToolMenu*) at ToolMenus.cpp:2119 as shown below

else if (GeneratedMenu->MenuType == EMultiBoxType::Menu)
{
        FMenuBuilder MenuBuilder(GeneratedMenu->bShouldCloseWindowAfterMenuSelection, GeneratedMenu->Context.CommandList, GeneratedMenu->Context.GetAllExtenders(), GeneratedMenu->bCloseSelfOnly, GeneratedMenu->StyleSet, GeneratedMenu->bSearchable, GeneratedMenu->MenuName);

        if (GeneratedMenu->StyleName != NAME_None)
        {
            MenuBuilder.SetStyle(GeneratedMenu->StyleSet, GeneratedMenu->StyleName);
        }

        MenuBuilder.SetExtendersEnabled(GeneratedMenu->bExtendersEnabled);
        PopulateMenuBuilder(MenuBuilder, GeneratedMenu);
        TSharedRef<SWidget> Result = MenuBuilder.MakeWidget(nullptr, GeneratedMenu->MaxHeight);
>>>>    GeneratedMenuWidget.Widget = Result;
        return Result;
}

This crash happens because the GeneratedMenuWidget was destroyed by UToolMenus::AddReferencedObjects within the callstack of PopulateMenuBuilder two lines above the crash.

Have Comments or More Details?

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

0
Login to Vote

Fixed
ComponentUE - Editor - Workflow Systems
Affects Versions5.4
Target Fix5.4
Fix Commit30042012
Main Commit30042071
CreatedNov 24, 2023
ResolvedDec 1, 2023
UpdatedJan 19, 2024