Add referenced object for map of UStructs


I understood a few things about Unreal garbage collector but here I’m stuck.
I have a UObject derived class holding a TMap to UStruct by value:

struct FSomeStruct
  float value1;
  float value2;

class USomeCollectionManager : public UObject
	TMap<SomeKey, FSomeStruct> Collection;

The “Collection” member is regularly wiped out by the GC (which I can verify by explicitly calling it using “obj gc”).

The issue is that:

  • I cannot make the map a UPROPERTY since they do not handle maps
  • I cannot use AddReferencedObjects since neither the map nor the FSomeStruct is a UObject

What would be the best thing to do here, in order to force the map to be referenced by its owning class and prevent it to be wiped out?

Indeed I just figured that out, but somehow deleted the question instead of replying it…Thanks!


There is no problem with the USomeCollectionManager as written. ‘Collection’ does not need to be a property. It sounds like the GC thinks your USomeCollectionManager object is not being referenced, and so it’s being collected, and so the Collection member is being destroyed.

You need to root a pointer to USomeCollectionManager somehow, so that’s known to the GC, probably by having:

USomeCollectionManager* Manager;

… in your module class, or by calling


… just after construction (and ->RemoveFromRoot() if you want it to be GC’d again).