TMap as UPROPERTY() seems to be fatally bugged in GarbageCollector.cpp

I posted this problem on a fatal crash that I’d get every time I tried running a packaged game. I finally discovered the reason why I kept getting this stack:
[FONT=Helvetica Neue]GarbageCollection.cpp [Line: 1894] Unknown token type (8) when trying to add ARO token.

Type 8 of EGCReferenceType i[FONT=Helvetica Neue]s GCRT_AddTMapReferencedObjects
This made me realize that I recently added UPROPERTY to one of my TMap variables.

The one in question that caused the issue was:
TMap<UWidget*, UBaseNavigableMenuStructure*> NavigationMap;

UBaseNavigableMenuStructure has parent UObject.

I wanted to post here for double coverage as answerhub is a bit of a black hole. This seems like a legit bug. In fact… it was too legit to quit, until I removed UPROPERTY().

To my latest knowledge TMap isn’t supported as a UPROPERTY. To prevent TMap entries from being garbage collected, I believe an often used work around is to have a UPROPERTY TArray that stores a copy of the TMap entries.

4.8 announced initial support for TMaps as UPROPERTY:

Oh, thats a great idea! I hope it gets streamlined over time. :slight_smile:

Replying to this thread for visibility, as well as the AnswerHub link above:

These two commits should help TMap property support in the GC and reference chain:

https://github.com/EpicGames/UnrealEngine/commit/355a6a2946f84669de49f977dc392ff55391daa8
https://github.com/EpicGames/UnrealEngine/commit/db5bff18ffd524aea056487a029964b46cb6346e

Both of these fixes should be in 4.9 if you don’t want to integrate the source changes yourself.

Steve

Thanks Steve!

Cheers Steve. I have a quick question relating to this.

Am I right in assuming that TMap now supports UObject referencing for UObject* keys as well as UObject* values?
If so, what are the ramifications (if any) of using a UObject*-keyed map as a UPROPERTY, given that the keys would presumably be set to null if the pointed-to object was garbage collected? In my experience map keys need to be unique and immutable. How is this dealt with?

I wondered the same thing pre-4.8 about a TMap< TWeakObjectPtr< UObject >, FSomeType >, which I had come across at places in the engine code.

Oh my god, I’ve been banging my head for days. I also use TMap in my UI navigation and my widgets is being garbage collected and crashes the game. Until now apparently is still a problem.

You shouldn’t have to, but couldn’t you add these widgets to the root? And then you remove them “manually” when you remove entries from the TMap.

Using UE4.27.2 and the entries in a TMap are still getting garbage collected, even with UPROPERTY(). Unless there’s any more versions planned for UE4 I guess this will always be an issue.