Crashing Editor

I don’t know what the error is but the editor keep crashing if i change the code from

AScrollerGameHUD::AScrollerGameHUD()
{
    ConstructorHelpers::FClassFinder<UMainMenuWidget> WidgetAsset(TEXT("WidgetBlueprint'/Game/HUD/MainMenu.MainMenu_C'"));
    if (WidgetAsset.Succeeded())
    {
        WidgetClass = WidgetAsset.Class;
    }
    if (WidgetClass)
    {
        MainMenuWidget = CreateWidget<UMainMenuWidget>(GetWorld(), WidgetClass);
        if (MainMenuWidget)
        {
            MainMenuWidget->AddToViewport();
        }
    }
}

To this:

AScrollerGameHUD::AScrollerGameHUD()
{
    this->CreateMainMenu();
}

void AScrollerGameHUD::CreateMainMenu()
{
ConstructorHelpers::FClassFinder<UMainMenuWidget> WidgetAsset(TEXT("WidgetBlueprint'/Game/HUD/MainMenu.MainMenu_C'"));
    if (WidgetAsset.Succeeded())
    {
        WidgetClass = WidgetAsset.Class;
    }
    if (WidgetClass)
    {
        MainMenuWidget = CreateWidget<UMainMenuWidget>(GetWorld(), WidgetClass);
        if (MainMenuWidget)
        {
            MainMenuWidget->AddToViewport();
        }
    }
}

Can i ask why?

PS: I want to have a system that has the possibility to change the player hud from main menu -> gameplay hud -> death screen.

So i though that having 3 function that creates each one of those widget and then call them when necessary was a good idea. Maybe i’m wrong. Pretty new to UE4 and C++ coding.

You can’t create a widget like that in the HUD’s constructor. You need to do it somewhere else, like BeginPlay.

Well, actually it works. It stop working if i put the same code inside a function and the call it.