Crash after creating widget on death

Hi, I’ve been trying to create a widget after destroying my player and it doesn’t seem to be working.
Very confused here, hoping for some help.

UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), playerDeathExplosion, player->GetActorLocation()); // spawn death explosion

player->Destroy(); // kill player

APlayerController* controller = UGameplayStatics::GetPlayerController(GetWorld(), 0);

 // crash happens here

if (URespawnCPP* RespawnUserWidget = CreateWidget<URespawnCPP>(controller, RespawnWidget.LoadSynchronous()))
{
    controller->SetInputMode(FInputModeUIOnly());
    controller->bShowMouseCursor = true;
    RespawnUserWidget->AddToViewport();
}

error:
https://hasteb.in/axupilub.php

Seemed to be linked to you widget, could you try make a simple UMG one ?
Also, to be sure you are not destroying player character when he die, check if pointer is valid and not pending to kill

Same crash even with a simple widget calling ‘UUserWidget’ instead of my custom class.

What is strange is that call stack doesn’t show you code, but something like next tick

I don’t understand it either. Possible bug?

Can you load widget first and check whether it had been loaded before passing it to CreateWidget?

I can confirm it does load.

Probably not

try these things :
surround your method like this to disable compilation optimisation for that part of code ( easier to debug ) :
also load the class on a separated part, just to be sure

#pragma optimize("", off)
    void myClass::MyMethod()
    {
        UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), playerDeathExplosion, player->GetActorLocation()); // spawn death explosion
     
        player->Destroy(); // kill player
     
        APlayerController* controller = UGameplayStatics::GetPlayerController(GetWorld(), 0);
        auto widgetClass = RespawnWidget.LoadSynchronous()
        if(controller  && widgetClass )
        {
            if (URespawnCPP* RespawnUserWidget = CreateWidget<URespawnCPP>(controller, widgetClass ))
            {
                controller->SetInputMode(FInputModeUIOnly());
                controller->bShowMouseCursor = true;
                RespawnUserWidget->AddToViewport();
            }
        }
    }
#pragma optimize("", on)

I still get the same exact error…

can you get the crash with visual studio attached ? with new code i shared, what line does the crash ?

pretty sure i already encountered this one but can recall what was the problem …

instead of the controller can you pass “GetWorld()” in CreateWidget ?

also if it still crash, can you comment the "add to veiwport ?
if still crash, uncomment that and add “RespawnUserWidget->AddToRoot();” before the addtoViewport

doesn’t crash when I comment AddToViewport() but when I add the AddToRoot() + AddToViewport() it crashes.

Then i would need the code of your “URespawnCPP” class, last thing i can see is you using some code in there causing this ( maybe an image you load programatically ? )

I mean I loaded a UUserWidget widget non related to my URespawnCPP class and it also crashed… not sure how it can be much different.

then it’s related of when you use it, try to spawn it ingame with same code ?
You said when player die, but what happen if you don’t spawn it ? you go back to main menu or just stay there ?

You do have engine pdb and sources downloaded right ? could you browse the stack trace to find more info on what going on ?

If I don’t spawn the widget?
it just ends up leaving me there.

I don’t have the engine source because it’s way too big, I still have no idea of what’s going on.

Using Blueprints, if a widget was created inside the CharacterBP and the character is then destroyed - an error will occur.

The widget would have to be created elsewhere, in an asset that does not get destroyed.

I’m not a c++ guy, but I hope this may shed some light on what may be happening.

Where is the widget being created ??

In the character, so I can probably try somewhere else then.

yea, try creating the widget in the Level BP (or perhaps your player controller), and see if the same error occurs upon the character’s death. good luck