CreateWidget C++ causes engine crash

I am developing UserWidget which expands UCanvasPanel

class INVENTORY_API UInventoryPanel : public UCanvasPanel
{
private:
	UItemSlot *DemoSlot;
}

So, I am trying to add to this canvas some item slots from my UUserWidget UItemSlot

void UInventoryPanel::generateSlots()
{
	DemoSlot = CreateWidget<UItemSlot>(this, UItemSlot::StaticClass());
	UCanvasPanelSlot* CanvasSlot = AddChildToCanvas(DemoSlot);
}

After CreatingWidget I recieve next exception in logs:


ogOutputDevice: Error: === Handled ensure: ===
LogOutputDevice: Error: Ensure condition failed: ParentUserWidget && ParentUserWidget->WidgetTree [File:D:/Build/++UE4/Sync/Engine/Source/Runtime/UMG/Private/UserWidget.cpp] [Line: 2075]
LogOutputDevice: Error: Stack: 
LogOutputDevice: Error: [Callstack] 0x00007ffd23c2e349 UE4Editor-UMG.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffd23ae048f UE4Editor-UMG.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffd0d4028d2 UE4Editor-Inventory.dll!CreateWidget<UItemSlot,UInventoryPanel>() [d:\programs\ue_24\ue_4.25\engine\source\runtime\umg\public\blueprint\userwidget.h:1435]
LogOutputDevice: Error: [Callstack] 0x00007ffd0d40b1f3 UE4Editor-Inventory.dll!UInventoryPanel::SynchronizeProperties() [d:\projects\cppplugins\plugins\inventory\source\inventory\widgets\inventorypanel.cpp:51]
LogOutputDevice: Error: [Callstack] 0x00007ffd23b8617e UE4Editor-UMG.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffd23ac282f UE4Editor-UMG.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffd23b32b40 UE4Editor-UMG.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffd23ab91fc UE4Editor-UMG.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffd1409ba89 UE4Editor-UMGEditor.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffd1408662e UE4Editor-UMGEditor.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffd4b262005 UE4Editor-Slate.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffd4b241af5 UE4Editor-Slate.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffd4b2c6730 UE4Editor-Slate.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffd4b2afdeb UE4Editor-Slate.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffd4b2a210a UE4Editor-Slate.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffd93487def UE4Editor-ApplicationCore.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffd93475c87 UE4Editor-ApplicationCore.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffd9348a619 UE4Editor-ApplicationCore.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffd934717d0 UE4Editor-ApplicationCore.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdaebde858 USER32.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdaebde299 USER32.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffd9348bb34 UE4Editor-ApplicationCore.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ff7f1aa68c4 UE4Editor.exe!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ff7f1abba7c UE4Editor.exe!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ff7f1abbb5a UE4Editor.exe!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ff7f1ace31d UE4Editor.exe!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ff7f1ad145a UE4Editor.exe!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdae247034 KERNEL32.DLL!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdaf362651 ntdll.dll!UnknownFunction []

And the engine crashes with next exception

Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x00000310

UE4Editor_Engine
UE4Editor_Inventory!UItemSlot::RebuildWidget() [d:\projects\cppplugins\plugins\inventory\source\inventory\widgets\itemslot.cpp:13]
UE4Editor_UMG
UE4Editor_UMG
UE4Editor_UMG
UE4Editor_UMG
UE4Editor_UMG
UE4Editor_UMG
UE4Editor_UMG
UE4Editor_UMG
UE4Editor_UMG
UE4Editor_UMG
UE4Editor_UMG
UE4Editor_UMG
UE4Editor_UMGEditor
UE4Editor_UMGEditor
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_Slate
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_Slate
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_Slate
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_Slate
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_Slate
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_SlateCore
UE4Editor_Slate
UE4Editor_Slate
UE4Editor_Slate
UE4Editor_Slate
UE4Editor
UE4Editor
UE4Editor
UE4Editor
UE4Editor
kernel32
ntdll

How can I fix this problem?

UInventoryPanel is child of UWidget, the CreateWidget method requires child of UUserWidget or player controller as outer
Try this:

DemoSlot = CreateWidget<UItemSlot>(UGameplayStatics::GetPlayerController(GetWorld(), 0), UItemSlot::StaticClass());

Anyway, if you want to pass UInventoryPanel instance as outer, you must make sure that at least outer of the instance is child of UUserWidget

Ok, now it doesn’t crash the engine. But:

DemoSlot = CreateWidget<UItemSlot>(UGameplayStatics::GetPlayerController(GetWorld(), 0), UItemSlot::StaticClass());
	if(DemoSlot != nullptr)
	{
		UCanvasPanelSlot* CanvasSlot = AddChildToCanvas(DemoSlot);
	}else
	{
		UE_LOG(LogTemp, Warning, TEXT("Widget wasn't created"));
	}

This code, provides next while I try to place my widget in editor:

LogScript: Warning: Script Msg: A null object was passed as a world context object to UEngine::GetWorldFromContextObject().
LogTemp: Warning: Widget wasn't created
LogTemp: Warning: Construction in Editor
LogTemp: Warning: Widget wasn't created

I want to draw it in editor too. Probably world doesn’t exist in editor, so if it is possible to solve it?

Also, it works in Build. But I need to implement it in Engine too)

not sure, but try this:

Cast<UItemSlot>(UWidgetBlueprintLibrary::Create(this, UItemSlot::StaticClass(), GetOwningPlayer()))

It expectedly doesn’t work. I think, it’s because of GetOwningPlayer doesn’t exist in Editor. I checked in the documentation, CreateWidget<> uses UWidget as a parent Widget (not UUserWidget). But anyway I don’t understand why my origin code doesn’t work.

Yes, CreateWidget takes UWidget, but if you look at actual implementation of that method in /Engine/Source/Runtime/UMG/Private/UserWidget.cpp
You will find this:

So in your original code when you pass child of UWidget it expects outer of the instance to be UUserWidget or child of it.

While investigating this issue, you can also check, how you are instantiating UInventoryPanel. When instantiating UInventoryPanel, you should set UUserWidget as outer.