Where is ActorComponent's GC Reference managed?

Hello.

First of all, I ask for your understanding of using a translator due to my lack of English skills.

We thought that the GC Reference of the ActorComponent was managed by the Actor who owns the ActorComponent.

https://dev.epicgames.com/community/learning/knowledge-base/ePKR/unreal-engine-garbage-collector-internals

When I read the document, Outer is not related to GC and it seems important whether UPROPERTY marking or not.

In other words, the actor must have the actor components as UPROPERTY in order to manage the actor component’s GC Reference.

(Unless it is managed by the FReferenceCollector)

When you look at the Actor.h file, there are the following member variables.

UPROPERTY(Instanced)
TArray<TObjectPtr> InstanceComponents;
UPROPERTY(TextExportTransient, NonTransactional)
TArray<TObjectPtr> BlueprintCreatedComponents;
TSet<TObjectPtr> OwnedComponents;

Okay, I think it will probably be registered with BlueprintCreatedComponents or InstanceComponents. There are many ways to create an ActorComponent, but I wrote the test code for calling NewObject → RegisterComponents.

I created an ATestActor class that can be placed at the level and overridden the BeginPlay function as follows.

void ATestActor::BeginPlay()
{
	Super::BeginPlay();

	USceneComponent* SceneComponent = NewObject<USceneComponent>(this, USceneComponent::StaticClass());
	SceneComponent->RegisterComponent();
	
	TArray<UObject*> Referencees { SceneComponent };
	TArray<UObject*> Objects = FReferencerFinder::GetAllReferencers(Referencees, nullptr);
	for (UObject* Object : Objects)
	{
		UE_LOG(LogTemp, Log, TEXT("%s"), *Object->GetName());
		
		AActor* Actor = Cast<AActor>(Object);
		if (IsValid(Actor))
		{
			for (UActorComponent* Component : Actor->BlueprintCreatedComponents)
			{
				UE_LOG(LogTemp, Log, TEXT("Blueprint Created Components : %s"), *Component->GetName());
			}
			for (UActorComponent* Component : Actor->GetInstanceComponents())
			{
				UE_LOG(LogTemp, Log, TEXT("Instance Components : %s"), *Component->GetName());
			}
			for (UActorComponent* Component : Actor->GetComponents())
			{
				UE_LOG(LogTemp, Log, TEXT("Owned Component : %s"), *Component->GetName());
			}
		}
	}
}

When you placed ATestActor at the level and pressed Run, the results are as follows.

LogTemp: TestActor_UAID_107C61B31D5EE84202_1576081023
LogTemp: Owned Component : SceneComponent_0

SceneComponent is not part of the InstanceComponents, BlueprintCreatedComponents.
Where do you manage GC Reference?

1 Like

Thank you. I should have looked at the documents more carefully, i’m sorry.

I didn’t think it was going to go from there because there was no place to call that function. I thought it was simply a feature close to a helper.

The AddReferencedObjects function is registered in the UOBJECT_CPPCLASS_STATICFUNCTIONS_ALLCONFIGS macro.

This macro belongs to IMPLEMENT_CLASS and is used in the .gen.cpp file generated by UHT.

And that function is called in the GC process.

Thanks once again.