TSharedPtr to Fstring out of memory

Hey all,

I’m having an issue from getting a TSharedPtr to a normal FString to use in Slate.

Issue is as following:
I have a SComboBox> which has a .OnGenerateWidget() and .OptionsSource(), from a TArray i build the TArray> for the Combobox, refresh options(doesn’t crash on this point) and then when i click the SComboBox in the editor it breaks with error “Ran out of memory allocating 18446744073709551612 bytes with alignment 0”. Assuming it runs the binded OnGenerateWidget() when we click the SComboBox it crashes whenever I try to get the TSharedPtr to an FString.

The line it crashes at is “FString stringname = *InItem;”
I tried the following combos

FString stringname = **InItem; (Access violation)
FString stringname = *InItem.Get(); (Out of memory)
FString stringname = *InItem; (Out of memory)

Anyone any idea what i’m doing wrong here?

Slate code:

SNew(SBox).WidthOverride(150.0f).HAlign(HAlign_Left)
						[
							SAssignNew(WorkspaceCombo, SComboBox<TSharedPtr<FString>>)
							.OnGenerateWidget(this, &STogglWidget::GenerateProjects)
							.OptionsSource(&WorkspaceArray)
						]

Functions->

TSharedRef<SWidget> STogglWidget::GenerateProjects(TSharedPtr<FString> InItem)
{
	if (InItem.IsValid()) 
	{
		FString stringname = *InItem;
		UE_LOG(LogTemp, Error, TEXT("%s"), *stringname);
	}
	return SNew(STextBlock);
}

void STogglWidget::UpdateWorkspaces(TArray<FString> Workspaces) {
	
	WorkspaceArray.Empty();
	for (FString Workspace : Workspaces) 
	{
		WorkspaceArray.Add(MakeShareable<FString>(&Workspace));
		UE_LOG(LogTemp, Error, TEXT("%s"), **WorkspaceArray[0].Get());
	}
	WorkspaceCombo->RefreshOptions();
}

Because I can’t seem to put certain parts in codeblocks on the post here’s an image with the text, correct

User “Turfster” on Discord found the solution.

This line

WorkspaceArray.Add(MakeShareable<FString>(&Workspace));

is using the temporary FString “Workspace” of the for each loop.
The passed memory address is valid INSIDE of the UpdateWorkspaces function, but when used later in the GenerateProjects function (called by the SComboBox for each entry of the WorkspaceArray) the address is invalid, because the original FString is long gone.

The solution for this is creating a new FString and just passing it the value of the Workspace FString like this:

WorkspaceArray.Add(MakeShareable<FString>(new FString(Workspace)));