RealDocs

UActorComponent::AddReferencedObjects

function Engine Since 4.0
#include "Components/ActorComponent.h"
Access: public Specifiers: static

Description

Static GC hook that registers additional UObject references held by this component with Unreal's garbage collector. Override this (using the static form) to prevent custom UObject pointers from being collected.

Caveats & Gotchas

  • This must be a static function — do not make it virtual. The GC invokes it via `UClass::ClassAddReferencedObjects`, not through a vtable. Use `CastChecked<UMyComponent>(InThis)` to access the instance.
  • Forgetting to call `Super::AddReferencedObjects` will skip the parent class's reference registration, potentially allowing objects held by parent fields to be garbage collected unexpectedly.

Signature

static ENGINE_API void AddReferencedObjects(UObject* InThis, FReferenceCollector& Collector);

Parameters

Name Type Description Default
InThis UObject* The component instance being examined by the GC.
Collector FReferenceCollector& The garbage collector's reference collector; add any UObject references to this.

Return Type

void

Example

Register a manually-tracked UObject pointer C++
void UMyComponent::AddReferencedObjects(UObject* InThis, FReferenceCollector& Collector)
{
    UMyComponent* This = CastChecked<UMyComponent>(InThis);
    Collector.AddReferencedObject(This->ManuallyTrackedAsset);
    Super::AddReferencedObjects(InThis, Collector);
}

Version History

Introduced in: 4.0

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.