# Crash on CreateWidget and ConstructWidget

**URL:** <https://forums.unrealengine.com/t/crash-on-createwidget-and-constructwidget/119959>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [December 14, 2018, 2:12am UTC](https://forums.unrealengine.com/t/crash-on-createwidget-and-constructwidget/119959 "2018-12-14T02:12:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_0ba2eb3c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0ba2eb3c](https://forums.unrealengine.com/u/anonymous_user_0ba2eb3c)\
**Post date:** [December 14, 2018, 2:12am UTC](https://forums.unrealengine.com/t/crash-on-createwidget-and-constructwidget/119959/1 "2018-12-14T02:12:38Z")

</div>

I have a class UAbilityWidget that extends UUserWidget. Then I have a class called UAbilitiesTree which also inherits from UUserWidget.

So I want to create UAbilityWidgets and add them to a canvas procedurally on UAbilitiesTree’s NativeConstruct. So I tried CreateWidget and widgetTree-\>ConstructWidget and both crash after being executed (take a look at the callstack below).

Here’s my code:

```auto

    for (int i = 0; i < abilities.Num(); i++)
    {
        UAbilityWidget* widget = WidgetTree->ConstructWidget<UAbilityWidget>(UAbilityWidget::StaticClass(), FName::FName(*("AbilityWidget" + FString::FromInt(i) + "]")));
        //Also tried CreateWidget<UAbilityWidget>(canvas, UAbilityWidget::StaticClass());

        abilityWidgets.Add(widget);

        UCanvasPanelSlot* slot = canvas->AddChildToCanvas(widget);
    }

```

Any ideas? Thanks for any help!

[SPOILER]  
Callstack:

```auto

Access violation - code c0000005 (first/second chance not available)

UE4Editor_UMG!UWidgetTree::ForEachWidget() [d:\build\++ue4\sync\engine\source\runtime\umg\private\widgettree.cpp:123]
UE4Editor_UMG!UUserWidget::GetSlotNames() [d:\build\++ue4\sync\engine\source\runtime\umg\private\userwidget.cpp:938]
UE4Editor_UMG!INamedSlotInterface::SetNamedSlotDesignerFlags() [d:\build\++ue4\sync\engine\source\runtime\umg\private\components
amedslotinterface.cpp:50]
UE4Editor_UMG!UUserWidget::SetDesignerFlags() [d:\build\++ue4\sync\engine\source\runtime\umg\private\userwidget.cpp:1307]
UE4Editor_UMG!UUserWidget::Initialize() [d:\build\++ue4\sync\engine\source\runtime\umg\private\userwidget.cpp:376]
UE4Editor_UMG!UUserWidget::CreateInstanceInternal() [d:\build\++ue4\sync\engine\source\runtime\umg\private\userwidget.cpp:2109]
UE4Editor_UMG!UUserWidget::CreateWidgetInstance() [d:\build\++ue4\sync\engine\source\runtime\umg\private\userwidget.cpp:1975]
UE4Editor_UMG!UUserWidget::CreateWidgetInstance() [d:\build\++ue4\sync\engine\source\runtime\umg\private\userwidget.cpp:1992]
UE4Editor_Hivaneph!CreateWidget<UUserWidget,UWidgetTree>() [c:\program files\epic games\ue_4.21\engine\source\runtime\umg\public\blueprint\userwidget.h:1279]
UE4Editor_Hivaneph!UAbilitiesTreeWidget::NativeConstruct() [c:\users\dev-projects\ue4\hivaneph\hivaneph\source\hivaneph\ui\abilitiestreewidget.cpp:71]

```

[/SPOILER]

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_0ba2eb3c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0ba2eb3c](https://forums.unrealengine.com/u/anonymous_user_0ba2eb3c)\
**Post date:** [December 14, 2018, 4:14am UTC](https://forums.unrealengine.com/t/crash-on-createwidget-and-constructwidget/119959/2 "2018-12-14T04:14:02Z")

</div>

Ok so apparently the first parameter in CreateWidget (OwningObject) has to be the world in which you want to create the widget. So

```auto

UAbilityWidget* widget = CreateWidget<UAbilityWidget>(GetWorld(), abilityWidgetClass);

```

did the trick.

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [December 14, 2018, 9:41am UTC](https://forums.unrealengine.com/t/crash-on-createwidget-and-constructwidget/119959/3 "2018-12-14T09:41:03Z")

</div>

Just as a general practice rule - you should really use the Player Controller that should own (or is displaying) that widget, rather than creating it using a world reference.

CreateWidget\<USomeWidget\>(GetPlayerController(), WidgetClass)

---

<div class="post-metadata">

**Author:** ![bogdanspbm](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bogdanspbm/32/726230_2.png) [@bogdanspbm](https://forums.unrealengine.com/u/bogdanspbm)\
**Post date:** [February 13, 2022, 2:20pm UTC](https://forums.unrealengine.com/t/crash-on-createwidget-and-constructwidget/119959/4 "2022-02-13T14:20:10Z")

</div>

Hello. Can you give an advice, how to create widget in Editor? PlayerController doesn’t exist while I am editing blueprint, but using parent widget causes crash.
