'UTextBlock::SetText Stack variables becomes empty string in the UI

I am trying to set the TextBlock's value in c++ during construction. If I use a string allocated in stack, it does not shown in the UI during runtime. I suspect that, when the code leaves the scope, FString object "txt" destructed. Than the ItemName that refers to it shows empty string. However the line just below it works just fine. Whats the problem here ?

void UContentLibraryItem::NativeConstruct()
{
    Super::NativeConstruct();

    if (ItemName)
    {
        FString txt = ItemName->Text.ToString();
        if (txt.Len() > 15)
        {
            txt.MidInline(0, 12);
            txt += "...";
            
            ItemName->SetText(FText::FromString(txt)); // This line doesnt work and text in the ui becomes empty string.
            //ItemName->SetText(FText::FromString("This works"));
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source