The dreaded GetAddReferencedObjects assertion failure during Cooking

It’s the second time I’ve severely burned my noob self on this, so I thought I’d bring it up here since it’s un-google-able… again I wrote something like:

UClass1::UClass1()
{
    Instance = NewObject<UClass2>();
}

This “works”, as in, you’ll write it and use it just fine until hours, or days, or weeks later when you attempt to cook your assets for packaging… then you get this fun and obscure error: “Assertion failed: CppClassStaticFunctions.GetAddReferencedObjects() != nullptr [File:D:\build++UE5\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\GarbageCollection.cpp] [Line: 3293]”

I mostly want to post this here to make this error google-able (answer: don’t create objects in constructors). Secondly, does anybody have any thoughts or insights on this, specifically: as a long-time programmer it does not seem strange to me to create objects within a constructor. I get that UE is tracking object creation for GC… but is this a bug, or is allowing this something we should request as a feature? Or are there so many UE-isms that there is nothing to do but learn its special ways?

Thanks

Yes, this is an Unreal thing. Each UClass has a “default” object (called the CDO) which gets constructed when the game launches and is used as a template to construct other instances.