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:
Hide the current widget (UUserWidget::RemoveFromViewport)
Show the other widget (UUserWidget::AddToViewport)
Get the “time” field from the widget being shown (UUserWidget::GetWidgetFromName)
My development environment: Windows 10 1703 and Visual Studio 2017
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.
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.
time = Cast<UTextBlock>(widget2->GetWidgetFromName(TEXT("Time")));
time = Cast<UTextBlock>(widget1->GetWidgetFromName(TEXT("Time")));