How to resolve FString allocation crash?

Hello everyone,

I am having this crash that I have been trying to resolve for about a month now, but I just cannot figure out what is going on. Basically I am using a Rest UObject which has a private FString variable that it initializes to some value after making a connection.

Then I have a Actor which uses a UTextRenderComponent * for the text actor. In this file, I use the GetWorld()->GetTimerManager().SetTimer(timerHandle, this, &AHospText::UpdateDisplay, 10.0f, true) method to get the FString from the Rest UObject. The UpdateDisplay method calls BuildHospString() and this is where I am having a problem. Since I want to display the private FString member that the Rest object has, BuildHospString() calls rest->GetResult() and GetResult() returns that private FString. Then BuildHospString() returns this FString and in my UpdateDisplay() I call HospText->SetText(BuildHospString());

This works fine for about a 45 seconds but then I get a crash. I just cannot figure out where I am leaking resources because the crash has to do with memory allocation for the FStrings. Here is what I get. I even tried to pass the private FString as a reference
all the way to UpdateDisplay but still no luck. I would greatly appreciate your help as eliminating this problem will help me to finally finish this project.

Here is the cpp file pseudo setup:

AHospText::AHospText()
{
//false to improve performance
PrimaryActorTick.bCanEverTick = false;

HospText = CreateDefaultSubobject<UTextRenderComponent>(TEXT("Default"));
HospText->SetHorizontalAlignment(EHTA_Center);

    //size of lettering
HospText->SetWorldSize(20.0f);
RootComponent = HospText;

}

void AHospText::BeginPlay()
{
Super::BeginPlay();

    HospText->SetText(LOCTEXT("Welcome","HOSPITAL STRING!!"));

rest = NewObject<URestClient>();

GetData();

}

void AHospText::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );

}

I have commented out control stuff because in essence I need the SetTimer to work indefinitely updating the text

//updates display text
void AHospText::UpdateDisplay()
{

//if the timer was used, clear it
//if (timerUsed)
//{
	//GetWorld()->GetTimerManager().ClearTimer(timerHandle);
	//build the hosp str

	HospText->SetText(BuildHospString());
	
           //HospStr = FText::FromString(BuildHospString());
//}

//set the hosp text
//HospText->SetText(FText::FromString(BuildHospString()));

}

//builds the display string
FString AHospText::BuildHospString()
{
//GEngine->AddOnScreenDebugMessage(-1, 30.f, FColor::Red, result);

return rest->GetResult();

}

//uses RestClient.h to get Data from a Rest Endpoint

void AHospText::GetData()
{
processed is set to true when Rest client is established

if (processed == true)
{
	//GEngine->AddOnScreenDebugMessage(-1, 30.f, FColor::Red, "AUTH IS TRUE");
	//GEngine->AddOnScreenDebugMessage(-1, 30.f, FColor::Red, rest->GetResult());
	//timer will be used
	timerUsed = true;
	//20 second timer
	GetWorld()->GetTimerManager().SetTimer(timerHandle, this, &AHospText::UpdateDisplay, 10.0f, true);
}

}

#undef LOCTEXT_NAMESPACE

Here is the Crash Report:

MachineId:
EpicAccountId:

Fatal error: [File:D:\BuildFarm\buildmachine_++UE4+Release-4.11\Engine\Source\Runtime\Core\Private\GenericPlatform\GenericPlatformMemory.cpp] [Line: 139]
Ran out of memory allocating 18446744072564227002 bytes with alignment 0

UE4Editor_Core!FDebug::AssertFailed() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\runtime\core\private\misc\outputdevice.cpp:430]
UE4Editor_Core!FGenericPlatformMemory::OnOutOfMemory() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\runtime\core\private\genericplatform\genericplatformmemory.cpp:140]
UE4Editor_Core!FMallocTBB::Realloc() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\runtime\core\private\hal\malloctbb.cpp:99]
UE4Editor_Core!FHeapAllocator::ForAnyElementType::ResizeAllocation() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\runtime\core\public\containers\containerallocationpolicies.h:357]
UE4Editor_nantVrDemo_1494!TArray::CopyToEmpty() [d:\program files\epic games\4.11\engine\source\runtime\core\public\containers\array.h:2416]
UE4Editor_nantVrDemo_1494!AHospText::BuildHospString() [d:\nant-vr-demo\nant-vr-demo 4.11 - 2\source\nantvrdemo\hosptext.cpp:99]
UE4Editor_nantVrDemo_1494!AHospText::UpdateDisplay() [d:\nant-vr-demo\nant-vr-demo 4.11 - 2\source\nantvrdemo\hosptext.cpp:65]
UE4Editor_Engine!FTimerUnifiedDelegate::Execute() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\runtime\engine\public\timermanager.h:38]
UE4Editor_Engine!FTimerManager::Tick() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\runtime\engine\private\timermanager.cpp:483]
UE4Editor_Engine!UWorld::Tick() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\runtime\engine\private\leveltick.cpp:1236]
UE4Editor_UnrealEd!UEditorEngine::Tick() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\editor\unrealed\private\editorengine.cpp:1356]
UE4Editor_UnrealEd!UUnrealEdEngine::Tick() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\editor\unrealed\private\unrealedengine.cpp:370]
UE4Editor!FEngineLoop::Tick() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\runtime\launch\private\launchengineloop.cpp:2644]
UE4Editor!GuardedMain() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\runtime\launch\private\launch.cpp:142]
UE4Editor!GuardedMainWrapper() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\runtime\launch\private\windows\launchwindows.cpp:126]
UE4Editor!WinMain() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\runtime\launch\private\windows\launchwindows.cpp:200]
UE4Editor!__scrt_common_main_seh() [f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:264]
kernel32
ntdll

Probably your URestClient was garbage collected, and code is trying to access FString on invalid pointer. If you’re creating URestClient somewhere, use rest->AddToRoot() after object’s creation (after NewObject) to ensure object is not garbage collected, and rest->RemoveFromRoot() when it is no longer needed.

I posted the question on the forum and the thing was I had to add UProperty() for the URestClient * . Also my instantiation had to use (this).
Thank you for your contribution. I appreciate it.

I posted the question on the forum and the thing was I had to add UProperty() for the URestClient * . Also my instantiation had to use (this). Thank you for your contribution. I appreciate it.

Most likely this also means that URestClient was garbage collected, because it wasn’t referenced neither by UPROPERTY nor by “Root”. Glad that you’ve solved your problem.