Widget switching crashes editor

Hello everyone,

Attached is a simple project which demonstrates the problem. The project has 2 widgets. Each widget shows the name of the current widget and a button for switching to the other widget. An additional field shows the time since the level started. Running in the editor, the “widget switching” works fine initially. However, as the time crosses the 1 minute mark, the editor crashes when the button is pressed. It seems to crash in the functions AMyPawn::Button1 or AMyPawn::Button2 depending on the current widget. All the function does is:

  1. Hide the current widget (UUserWidget::RemoveFromViewport)
  2. Show the other widget (UUserWidget::AddToViewport)
  3. Get the “time” field from the widget being shown (UUserWidget::GetWidgetFromName)

My development environment: Windows 10 1703 and Visual Studio 2017

Sample Project

What line does your code crash on? Can I see the crash log?

Thank you for your reply, Reuben. Attached is the crash log.
link text

What line does it crash on? When your project crashes it should tell you the last line of code that was executed before the crash.

It crashes on line 59 of MyPawn.cpp. It is on line 245 of the log file.

Reuben, are you (or anyone following this) able to reproduce the problem with my sample project? I just wish to know if I am the only one having the problem or if there is something wrong with my setup/installation. Thank you.

It crashes on line 59 of MyPawn.cpp, but what is that line of code doing? Otherwise there isn’t really any insight into what could be going wrong

Thank you for spending time to try the sample. Actually, lines 52 and 59 get the UTextBlock from the current active widget so that the “time since level started” is displayed in AMyPawn::Tick. If you have time to try further, try commenting lines 52 and 59 and run again. Now time will only be displayed correctly in widget1 and the editor will still crash after 1 minute. Strangely, without line 52 and 59, the crash log will not even tell on which line it crashes. It must be either AMyPawn::Button1 or AMyPawn::Button2 because it crashes when a button is pressed. Another strange thing which I have totally no idea: why it only crashes after 1 minute?

Garbage collection is likely whats causing the issue since you’re trying to remove them from the viewport when switching widgets vs just changing their visibility. Try the following code and if that doesn’t work, let me know and I’ll look into it further.

void AMyPawn::Button1()
{
	widget1->SetVisibility(ESlateVisibility::Collapsed);
	widget2->SetVisibility(ESlateVisibility::Visible);
	time = Cast<UTextBlock>(widget2->GetWidgetFromName(TEXT("Time")));
}

void AMyPawn::Button2()
{
	widget2->SetVisibility(ESlateVisibility::Collapsed);
	widget1->SetVisibility(ESlateVisibility::Visible);
	time = Cast<UTextBlock>(widget1->GetWidgetFromName(TEXT("Time")));
}

Thank you Chris. It works.