Placement new for AActor spawning


I have been delving in the low level workings of the Engine for educational purposes. Specifically I am interested in the process of UWorld:SpawnActor. I can follow the C++ code till here

// actually make the actor object
AActor* const Actor = NewObject<AActor>(LevelToSpawnIn, Class, NewActorName, ActorFlags, Template, false/*bCopyTransientsFromClassDefaults*/, nullptr/*InInstanceGraph*/, ExternalPackage);

The function NewObject basically returns a static_cast of UObject like so

return static_cast<T*>(StaticConstructObject_Internal(Params));

UObject is an object, if I understand right, which has been initialised by placement new operator, which, in turn, calls the constructor of UObjectBase like so

new ((void *)Obj) UObjectBase(const_cast<UClass*>(InClass), InFlags|RF_NeedInitialization, InternalSetFlags, InOuter, InName, OldIndex, OldSerialNumber);

Seems alright and makes sense to me till here.

I am wondering where is the placement new written for the actual AActor that we are spawning without which there won’t be any constructor call of AActor class and consequently no vtable, rendering virtual functions of the AActor class useless, to name a issue I can fathom.

Thanks for reading,

StaticConstructObject_Internal calls StaticAllocateObject which performs the placement new. After that function returns StaticConstructObject_Internal then calls the constructor indirectly via the ClassConstructor function pointer which is a member of UClass: (*InClass->ClassConstructor)(FObjectInitializer(Result, Params));

If you follow where ClassConstructor is initialized you’ll find the IMPLEMENT_CLASS macro which is used throughout the generated code. The macro contains GetPrivateStaticClassBody which ultimately calls the constructor of UClass. It gets passed the InternalConstructor<TClass> which just calls T::__DefaultConstructor(X); which does another placement new new((EInternal*)X.GetObj())TClass;.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.