Confusion on TMaps, Garbage Collection

I’m working on a system where I want to load a series of datatables, each with corresponding to a scripted conversation in my game. I think keep a list of all those assets in a TMap with their identifier and a soft object pointer.

That said, I appear to be running into garbage collection troubles - there’s either something about TMaps and garbage collection that I’m not understanding, or perhaps I’m misusing soft object pointers and should be using another method for pointing to the loaded object to prevent this from happening. Thoughts?

Here’s some sample code:

// In my .h file, declarations:

UPROPERTY()
TMap<int, TSoftObjectPtr> ChapterConvoAssets;

// Loading the asset from an asset manifest:

// Load Convos

for (auto& Entry : NewAssets->ChapterConvoAssets)

{
FSoftObjectPath TablePath = FSoftObjectPath(*Entry.Value);

TSoftObjectPtr<UDataTable> Table = Cast<UDataTable>(TablePath.TryLoad());

if (Table)
{
	ChapterConvoAssets.Add(Entry.Key,Table);

	UE\_LOG(LogTemp, Warning, TEXT("Loaded convo asset: %s"), \*Entry.Value)
}

}

Well yes you are loading them into soft object pointers, so GC will clear them up first chance it gets.
TMap is not at fault here, why not store them into TMap<int32, UDataTable*> ? This should keep them alive as long as they are in the TMap.
When you don’t need them anymore, clear the map with ChapterConvoAssets->Empty() and then gc will clean them up automatically.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.