Download

UE4 Delegate, Suddenly unbounded in runtime

Hello, In my project, there is a class which has a delegate as a member variable.
My CharacterClass has that class and some function would be bound with the delegate in the class.

Q. Suddenly, the binding is unbound. What happened to this? It seems that GC activated but I don’t understand clearly.

Here is my ClassA.h

UCLASS()
class SECONDARYPRACTICE2_API UClassA : public UObject
{
	GENERATED_BODY()
public:
	FVoidNotier Notier; //FVoidNotier is a delegate created by DECLARE_DELEGATE
};

Here are my CharacterClass’s BeginPlay( ) and Tick( )

void ASecondaryPractice2Character::BeginPlay()
{
	Super::BeginPlay();
	ClassA = NewObject<UClassA>();
	ClassA->Notier.BindLambda([=]() {UE_LOG(LogTemp, Warning, TEXT("Calling from ClassA")); });
}

void ASecondaryPractice2Character::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	if (ClassA == nullptr)
	{
		UE_LOG(LogTemp, Warning, TEXT("ClassA is Null"));
	}
	else
	{
		if (ClassA->Notier.IsBound() == false)
		{
			UE_LOG(LogTemp, Warning, TEXT("ClassA's Delegate is not bound@@@@"));
		}
		else
		{
			ClassA->Notier.ExecuteIfBound();
		}
	}
}

As long as ClassA is not null and ClassA’s Delegate is bound, “Calling from ClassA” would be printed. But suddenly, “ClassA’s Delegate is not bound@@@@” is being printed…
UnboundSample

What happened to this?

Is the ClassA property declared UPROPERTY() ?

No It is not. However, because ClassA is still not nullptr, I thought ClassA wasn’t collected by GC so ClassA’s delegate’s binding was still safe. And I am going to store those kinds of ClassA in TMap.
Anyway, I will try put UPROPERTY on the ClassA.
Thank you.

If your pointer isn’t UPROPERTY than GC doesn’t know your pointer exists, so if there’s no UPROPERTY pointers holding a reference to it, it will garbage collect.

Thank you. You are right.
With UPROPERTY, Even though I called GC manually It wasn’t collected.
One thing that I wonder is that if the delegate’s binding is a target of GC.
Anyway thank you again!