C++ UPROPERTY actor component variable being set to null

This is most likely due to the behavior of uobject construction to set UPROPERTY variables to a copy of the “archetype” object from GetDefaultObject for the class. This happens AFTER the C++ constructor is called. In my case, I had runtime-created UTexture2D objects as UPROPERTYs so they wouldn’t be garbage collected, but the variables were being set back to nullptr right after the constructor.

This may differ depending on how the object is constructed. The object in my case is an actor in a level. I haven’t solved this yet but I’m assuming there’s a UPROPERTY specifier that will will change this. The bCopyTransientsFromClassDefaults value below is false.

// UObjectGlobals.cpp FObjectInitializer::PostConstructInit()
// UE 5.4.4
	if (bShouldInitializePropsFromArchetype)
	{
		UClass* BaseClass = (bIsCDO && !GIsDuplicatingClassForReinstancing) ? SuperClass : Class;
		if (BaseClass == NULL)
		{
			check(Class==UObject::StaticClass());
			BaseClass = Class;
		}
	
		UObject* Defaults = ObjectArchetype ? ObjectArchetype : BaseClass->GetDefaultObject(false); // we don't create the CDO here if it doesn't already exist
		InitProperties(Obj, BaseClass, Defaults, bCopyTransientsFromClassDefaults);
	}