Download

How do I avoid a crash when trying to SEditableTextBox::SetStyle()

I have tried my best to figure this one out, I even started learning how to use the debugger but I still don’t understand what I am doing wrong.

I am trying to set the style on a SEditableTextBox but the way that I am trying to do that results in a crash.

The RobotoFont UFont is being fed into SCheatMenuWidget by a slate argument at creation from the MenuHUD. This was changed from how you will see Cactus Font loaded in SCheatMenuWidget.cpp

MenuHUD.cpp

AMenuHUD::AMenuHUD()
{
    static ConstructorHelpers::FObjectFinder<UFont> FontObject(TEXT("Font'/Engine/EngineFonts/Roboto.Roboto'"));
    if(FontObject.Object)
    {
        RobotoFont = FontObject.Object;
    }
}

SCheatMenuWidget.cpp

void SCheatMenuWidget::Construct(const FArguments& InArgs) 
{
     bCanSupportFocus = true;

     OwningHUD = InArgs._OwningHUD;
     MyGameInstance = InArgs._MyGameInstance;
     RobotoFont = InArgs._RobotoFont;

     // FONT SETTINGS
     // Cactus Font
     const FCompositeFont FontToChoose = FCompositeFont(FName(TEXT("Cactus")), FPaths::ProjectContentDir() / "Fonts/Cactus.otf",
     EFontHinting::Default, EFontLoadingPolicy::LazyLoad);

     TSharedPtr<const FCompositeFont> ChosenFont(new FCompositeFont(FontToChoose));
     // Roboto Font
     if(!RobotoFont.IsValid()) {UE_LOG(LogTemp, Error, TEXT("RobotoFont is not valid!")); return;}
    const FCompositeFont CheatFontToChoose = RobotoFont->CompositeFont;

     TSharedPtr<const FCompositeFont> ChosenCheatFont(new FCompositeFont(CheatFontToChoose));
 //......
 //Irrelevant code
 //.....
     FFontOutlineSettings TextOutline;
     TextOutline.OutlineColor = FColor::Black;
     TextOutline.OutlineSize = 1;
     // Default Text Style
     FSlateFontInfo ButtonTextStyle = FCoreStyle::Get().GetFontStyle("EmbossedText");
     ButtonTextStyle.CompositeFont = ChosenFont;
     ButtonTextStyle.Size = 40.f;
     ButtonTextStyle.OutlineSettings = TextOutline;
    ButtonTextStyle.TypefaceFontName = TEXT("Cactus");

     // Cheat Style Settings
    FSlateFontInfo CheatTextStyle = ButtonTextStyle;
     CheatTextStyle.FontObject = Cast<UObject>(RobotoFont); // This was to try to resolve the issue. Probably wrong.
     CheatTextStyle.CompositeFont = ChosenCheatFont;
     CheatTextStyle.TypefaceFontName = TEXT("Roboto");

     const FEditableTextBoxStyle TextBoxStyle = FEditableTextBoxStyle()
     .SetFont(CheatTextStyle)
     .SetPadding(FMargin(0.f));
//.....
//.....
  // Cheat Text Box
             + SConstraintCanvas::Slot()
             .Anchors(FAnchors(.4f, .6f, .6f, .45f))
             [
                 SNew(SScaleBox)
                 .HAlign(HAlign_Center)
                 .VAlign(VAlign_Center)
                 .Stretch(EStretch::ScaleToFit)
                 [
                     SAssignNew(CheatBox, SEditableTextBox)
                     .OnTextCommitted(this, &SCheatMenuWidget::OnCheatEntered)
                     .Style(&TextBoxStyle) // Commenting this out resolves the crash.
                     //.Style(TextBlockStyle) // Does not compile.
                 ]
             ]

This is my crash report

1. Unhandled Exception: EXCEPTION_ACCESS_VIOLATION writing address 0x00000000c0fc0008* UE4Editor_SlateCore!FSlateFontInfo::FSlateFontInfo()
2. UE4Editor_Slate!SEditableTextBox::DetermineFont() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Slate\Public\Widgets\Input\SEditableTextBox.h:422]
3. UE4Editor_Slate!TBaseSPMethodDelegateInstance<1,SEditableTextBox const ,0,FSlateFontInfo __cdecl(void),FDefaultDelegateUserPolicy>::Execute() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Core\Public\Delegates\DelegateInstancesImpl.h:290]
4. UE4Editor_Slate!TAttribute<FSlateFontInfo>::Get() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Core\Public\Misc\Attribute.h:160]
5. UE4Editor_Slate!SEditableText::SynchronizeTextStyle() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Slate\Private\Widgets\Input\SEditableText.cpp:431]
6. UE4Editor_Slate!SEditableText::CacheDesiredSize() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Slate\Private\Widgets\Input\SEditableText.cpp:116]
7. UE4Editor_SlateCore!SWidget::Prepass_Internal() [D:\Build\++UE4\Sync\Engine\Source\Runtime\SlateCore\Private\Widgets\SWidget.cpp:1544]
8. UE4Editor_SlateCore!SWidget::Prepass_Internal() [D:\Build\++UE4\Sync\Engine\Source\Runtime\SlateCore\Private\Widgets\SWidget.cpp:1528]
9. UE4Editor_SlateCore!SWidget::Prepass_Internal() [D:\Build\++UE4\Sync\Engine\Source\Runtime\SlateCore\Private\Widgets\SWidget.cpp:1528]
10. UE4Editor_SlateCore!SWidget::Prepass_Internal() [D:\Build\++UE4\Sync\Engine\Source\Runtime\SlateCore\Private\Widgets\SWidget.cpp:1528]
11. UE4Editor_SlateCore!SWidget::SlatePrepass() [D:\Build\++UE4\Sync\Engine\Source\Runtime\SlateCore\Private\Widgets\SWidget.cpp:646]
12. UE4Editor_Slate!SScaleBox::CustomPrepass() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Slate\Private\Widgets\Layout\SScaleBox.cpp:67]
13. UE4Editor_SlateCore!SWidget::Prepass_Internal() [D:\Build\++UE4\Sync\Engine\Source\Runtime\SlateCore\Private\Widgets\SWidget.cpp:1510]
14. UE4Editor_SlateCore!SWidget::Prepass_Internal() [D:\Build\++UE4\Sync\Engine\Source\Runtime\SlateCore\Private\Widgets\SWidget.cpp:1528]
15. UE4Editor_SlateCore!SWidget::Prepass_Internal() (Many Identical Lines Removed)[D:\Build\++UE4\Sync\Engine\Source\Runtime\SlateCore\Private\Widgets\SWidget.cpp:1528]
16. UE4Editor_SlateCore!SWidget::SlatePrepass() [D:\Build\++UE4\Sync\Engine\Source\Runtime\SlateCore\Private\Widgets\SWidget.cpp:646]
17. UE4Editor_Slate!PrepassWindowAndChildren() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Slate\Private\Framework\Application\SlateApplication.cpp:1139]
18. UE4Editor_Slate!FSlateApplication::DrawPrepass() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Slate\Private\Framework\Application\SlateApplication.cpp:1188]
19. UE4Editor_Slate!FSlateApplication::PrivateDrawWindows() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Slate\Private\Framework\Application\SlateApplication.cpp:1235]
20. UE4Editor_Slate!FSlateApplication::DrawWindows() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Slate\Private\Framework\Application\SlateApplication.cpp:1017]
21. UE4Editor_Slate!FSlateApplication::TickAndDrawWidgets() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Slate\Private\Framework\Application\SlateApplication.cpp:1569]
22. UE4Editor_Slate!FSlateApplication::Tick() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Slate\Private\Framework\Application\SlateApplication.cpp:1423]
23. UE4Editor!FEngineLoop::Tick() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Launch\Private\LaunchEngineLoop.cpp:4948]
24. UE4Editor!GuardedMain() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Launch\Private\Launch.cpp:169]
25. UE4Editor!GuardedMainWrapper() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Launch\Private\Windows\LaunchWindows.cpp:137]
26. UE4Editor!WinMain() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Launch\Private\Windows\LaunchWindows.cpp:268]
27. UE4Editor!__scrt_common_main_seh() [d:\agent\_work\5\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288]
28. kernel32
29. ntdll

If you need more information please let me know! :smile: Thanks in advance!

For slate style and style sets they’re a bit more complicated than just making it I believe. You could try turning that variable into a shared ref or shared pointer. Otherwise you’ll need to look at how the engine creates styles. There’s stuff like SlateEditorStyle and or CoreStyle, check out the create methods and where they’re called

Thanks so much for pointing me in the right direction!

I will definitely try playing around with shared refs and pointers, I’ll also double check the methods to make sure I’m not doing anything silly. Thanks again!

You have a crash because you are passing the address of a local variable(TextBoxStyle) which is destroyed when Construct function exits.
Store it as a class member or create your own style management class.

BTW I don’t know if the whole process of creating the font is correct.

1 Like

Thank you so much! I stored it as a class member as you recommended and that fixed the crash.

Thanks for the heads up about the font, things got quite weird in my attempted fixes. I will change that to how I have been shown to deal with fonts in Slate.