Creating a blueprint UserWidget in c++ crashes

Nneeds before encountering a problem:
I need to create blueprint asset UserWidgets in c++, Widgets are created and implemented entirely from blueprints, I can customize the number and layout of widgets in c++;
Since ContructObjFormClass() in the blueprint can’t build the Widgets directly, I moved to implement them inside c++

My implementation idea:
First create a SDynamicWidget that inherits from SVerticalBox, load the blueprint asset and get his Class via LoadClass<>() in the Construct() function

UClass* TitleClass = LoadClass<UUserWidget>(WorldContent, WG_TITLE);

Then NEW a sample blueprint

UUserWidget* BlueprintInstance = NewObject<UUserWidget>(WorldContent, TitleClass);

Since the RootWidget of this object is Null, I initialize it and add it to the VerticalBox::FSlot.

if (BlueprintInstance)
        AddSlot()
	[
		BlueprintInstance->GetRootWidget()->TakeWidget()
	]					
}

Then this UWidget will display the created UI:

BUT:
When the widget AddToViewport(), runs again for about 30 seconds before crashing:
image


I can’t find out anything about it on the call stack.


Restoration step:

  1. New UserWidget Asset
    image
  2. Add any component
    image
  3. New c++Class Public UWidget And SClass Public SVerticalBox
  4. Loading blueprint assets in SVerticalBox subclasses
UClass* PClass= LoadClass<UUserWidget>(nullptr, "/Game/Vendor/DynamicWidget/NewWidgetBlueprint.NewWidgetBlueprint_C'");
UUserWidget* BlueprintInstance = NewObject<UUserWidget>(WorldContent, PClass);
  1. Add Blueprint Widget to Slot
BlueprintInstance->Initialize();
AddSlot()
	[
		BlueprintInstance->GetRootWidget()->TakeWidget()
	]
  1. Use it and AddToViewport
    image
  2. Start Play, wait for crash :sweat_smile:

Engine version:4.27
UE5 didn’t change the UserWidget a lot, so it should be able to restore this in UE5 as well!