I'm a little confused about the use of AddWeakLambda in UDebuffNiagaraComponent. My understanding from the course was that the weak lamda was necessary so that we don't increment the reference count of the character class and suppress garbage collection.
To monitor garbage collection I added a destructor to both the character base class and the debuff component. I was curious to see if the the following call that Stephen added would prevent garbage collection:
CombatInterface->GetOnDeathDelegate().AddDynamic(this, &UDebuffNiagaraComponent::OnOwnerDeath);
Everything destructed properly about 30 seconds after killing the character. All good.
I then tried using both AddWeakLamda and AddLamda for monitoring ASC late creation (outside if statement so it was always registering the callback). In both cases, both the component and character were garbage collected properly. So it seems that UE works properly even if you use a normal lamda instead of a weak one.
if (CombatInterface)
{
CombatInterface->GetOnDeathDelegate().AddDynamic(this, &UDebuffNiagaraComponent::OnOwnerDeath);
// Try adding a normal lambda
CombatInterface->GetOnASCRegisteredDelegate().AddLambda([this](UAbilitySystemComponent* InASC)
{
InASC->RegisterGameplayTagEvent(DebuffTag, EGameplayTagEventType::NewOrRemoved).AddUObject(this, &UDebuffNiagaraComponent::DebuffTagChanged);
});
}
What am I missing here? Is my understanding of the purpose of AddWeakLamda incorrect?