Using shared pointers to reference UObjects in UE 5-0EA?

So… in UE 5-0EA, I’m using subclasses of UActorComponent to add configuration information to my AActor subclass. I want to be sure that when the UAC sub goes away that the corresponding configuration can be invalidated.

To that end, I’d store a pointer to the UAC sub in the array that the AA sub keeps. But I’m having trouble tracking lifetime of the UAC subs — and I’m wondering if I should keep a shared pointer to the UAC sub instead.

Is that Valid? Is that Necessary?

In general, I’d like the UAC subs to add their flavour to the AA sub as they arrive and remove it as they go. Is there a better way to manage this?

In UE4 smart pointers are not compatible with UObjects and derived types (including Components and Actors). You let the GC do its thing, and you have the option of explicity destroying components and actors if that’s what you want to do. A UPROPERTY TArray<UObject*> will keep referenced objects alive, and individual entries will be nulled out if you manually destroy the referenced object.

I’m not super familiar with UE5, but my understanding is that not much has changed wrt object lifetimes and GC.

When talking about components specifically, they have some callbacks for when they’re registered and unregistered with an actor: OnRegister() & OnUnregister(). So maybe all you need to do is override OnUnregister() to do something when your component is removed from the actor.

1 Like