Question about Garbage Collection

Hello guys,

I have read the following guides on Unreal Garbage Collection:

Official Documentation

Unreal Wiki

I am left with the impression that every class field (member) should be marked with UPROPERTY()
However there is one case that is bothering me and I am not sure about. Imagine we have this:

UCLASS()
class LEARNINGGROUND_API UCharacterAnimationInstance : public UAnimInstance
{
	GENERATED_BODY()
	
public:	

	void NativeInitializeAnimation() override;

private:

	APawn* Owner = nullptr;
};

And the implementation of NativeInitialzeAnimation() is like so:

void UCharacterAnimationInstance::NativeInitializeAnimation()
{
	this->Owner = this->TryGetPawnOwner();
}

Now the question is, since we don’t initialize the Owner in this fashion, for example:

this->Owner = NewObject();

Do we have to mark the raw pointer Owner with UPROPERTY()? My guess is no, because of the RAII principle. In other words the raw pointer is pointing to something that is already managed elsewhere. Is this correct?

Cheers.

UPROPERTY is for properties you want to serialize, expose in the Editor, and have Unreal handle all the dynamic allocation, reference resolves, and such for. It’s completely fine to have a naked pointer that you are assigning at run-time. A pointer is just an 32/64bit integer (depending on your platform) that contains a memory address, so it doesn’t inherently need garbage collection.

Since you are just storing an address to an object that is already instantiated elsewhere, you’re fine.

I guess it will be better to use TWeakObjectPtr Owner instead. You can use naked pointers safely only when your class is dependant from something it points to by its lifetime. Like some class and it’s parent. The trouble you can get with raw pointer points to the Pawn owner is when pawn disconnects GC will remove actual pawn object and you’ll get a broken pointer. TWeakObjectPtr will became null automatically at this situation.
But you can use raw pointers to the hierarchical parent of component for example (because when parent object will be GC-ed all children will be removed too).

Thank you, I got confused by some sentences in the guides that pointed out that “Every member should be marked with UPROPERTY”. I suppose that the garbage collector is pretty smart and can handle such general cases as the one I discribed. My concern was that since that raw pointer points to something that is managed elsewhere and it’s not marked with UPROPERY actually the collector will try to delete what it points to and will screw things up.

Thank you very much for the reply. I wasn’t aware that Unreal has it’s own smart pointers. Will check this out.

Yes, it has: TSharedPtr, TSharedRef, TWeakPtr for unmanaged objects and TWeakObjectPtr for managed object. Also there is a decorator called FGCObject. If you inherit your unmanaged class from it you can override method AddReferencedObjects and send UObjects stored somewhere in raw pointers to GC to prevent them from deletion.