Why does UE4 sometimes create and delete extra versions of my actor (e.g. SKEL)?

I’m trying to implement a lazy singleton by setting a pointer in the constructor and clearing it in the destructor. This works most of the time, but on some runs, it will create extra versions of the actor (specifically, it creates Default__<name>, Default__SKEL_<name>, and then Default__<name> again before finally creating the real one) and then those objects will be deleted by the GC a short while later. This then causes the pointer to be cleared.

Anyone know what’s going on? Is there a way to disable these extra spawns?

For the record, I just moved my stuff into Begin/EndPlay, but I’m still curious as to what’s going on here and why.

Can’t really answer your question, but if you are dealing with singletons, take a look at Subsystems.

You can’t treat UObjects in general (and especially actors) as normal C++ classes. All loaded UObject classes have a “default object” in memory, which is the Class Default Object (CDO). All other instantiations of that class are just copies of the CDO, which generally shouldn’t be modified. Definitely don’t rely on the timing or frequency of constructor and destructor calls. I’m not sure what the situation is for AActors, though I’m pretty sure they’ll have a CDO, but it wouldn’t surprise me if Unreal is creating several additional objects in the background.

Great. Thanks for the info. Out of curiosity, any idea why it’s creating the skeleton version for an actor that doesn’t have a mesh or anim component?

SKEL isn’t the skeleton for animated characters, it’s the “skeleton” for your class object. Basically the starting point of the object during class recompilation (through Blueprints). It roughly goes like this: You change something on your Blueprint and hit Compile -> SKEL_YourClass_<Number> is created -> Details are filled out -> CDO_YourClass_<Number> is created -> Old class properties are copied to the new class -> Old class instances are replaced with the new class instance -> SKEL class is cleaned up.

Very informative. Thanks!