What prevents class default objects from being garbage collected?

Class default objects are not part of the root set (CDO->IsRooted() returns false) but their enclosing class is (A UClass instance returns IsRooted() true), however the ClassDefaultObject member of UClass is not marked a property:

class COREUOBJECT_API UClass : public UStruct
{
    /*...*/

    UObject* ClassDefaultObject; // <-- not a UPROPERTY
}

They are created by UClass::CreateDefaultObject() which calls:

ClassDefaultObject = StaticAllocateObject(this, GetOuter(), NAME_None, EObjectFlags(RF_Public|RF_ClassDefaultObject|RF_ArchetypeObject));

I’m not sure what StaticAllocateObject does differently to NewObject, or if it’s relevant.

So I’m curious - what prevents class default objects from being garbage collected? Anyone know?

(Maybe the GC checks for the RF_ClassDefaultObject object flag or something?)

2 Likes