TimerDelegate Create UObject or Lambda cause read access violation after a while

SilentGodot
Fri, 21 May 2021 22:49:47 +0000
C++

I set up a timer delegate in the following way (Inside a GameplayAbility class if it matters):

  if (AttackProps.HitCount > 1)
    		{
    			MultihitDelegate = FTimerDelegate::CreateLambda([this,
    															hitbox,
    															AttackProps,
    															Target_ASC,
    															TargetChar,
    															DamageEffectSpecHandle,
    															KnockbackEffectSpecHandle] ()
    			{
    				this->ApplyMultihit(hitbox,
    									AttackProps,
    									Target_ASC,
    									TargetChar,
    									DamageEffectSpecHandle,
    									KnockbackEffectSpecHandle);
    			});
    			FTimerManager &timeMan = GetWorld()->GetTimerManager();
    			timeMan.SetTimer(
    				MultihitHandle, MultihitDelegate, AttackProps.MultihitDelay, true);

Inside the ApplyMultihit function I do:

// If we are done multihitting
if (hitbox->GetHitCount(TargetChar) >= AttackProps.HitCount) {
	GetWorld()->GetTimerManager().ClearTimer(MultihitHandle);
	MultihitDelegate.Unbind();
	OnCombatEffectsDone.Broadcast();
}

After a few times where the timer function behaves as expected, eventually I reach an exception which crashes my project.

call stack:

[Inline Frame] UE4Editor-Tosh_Prototype.dll!FDelegateBase::Allocate(int) Line 117 (e:\Program Files\Epic Games\UE_4.26\Engine\Source\Runtime\Core\Public\Delegates\DelegateBase.h:117)
UE4Editor-Tosh_Prototype.dll!operator new(unsigned __int64 Size, FDelegateBase & Base) Line 137 (e:\Program Files\Epic Games\UE_4.26\Engine\Source\Runtime\Core\Public\Delegates\DelegateBase.h:137)
UE4Editor-Tosh_Prototype.dll!TBaseFunctorDelegateInstance<void __cdecl(void),FDefaultDelegateUserPolicy,void <lambda>(void) >::CreateCopy(FDelegateBase & Base) Line 825 (e:\Program Files\Epic Games\UE_4.26\Engine\Source\Runtime\Core\Public\Delegates\DelegateInstancesImpl.h:825)
UE4Editor-Tosh_Prototype.dll!TDelegate<void __cdecl(void),FDefaultDelegateUserPolicy>::operator=(TDelegate<void __cdecl(void),FDefaultDelegateUserPolicy> && Other) Line 382 (e:\Program Files\Epic Games\UE_4.26\Engine\Source\Runtime\Core\Public\Delegates\DelegateSignatureImpl.inl:382)
UE4Editor-Tosh_Prototype.dll!UCombatEffectApplier::ApplyHit(UCombatHitboxComponent * hitbox, FAttackProperties AttackProps, UAbilitySystemComponent * Target_ASC, const ACombatCharacterBase * TargetChar, FGameplayEffectSpecHandle DamageEffectSpecHandle, FGameplayEffectSpecHandle KnockbackEffectSpecHandle) Line 25 (e:\Projects\Gamedev\Tosh_Prototype\Source\Tosh_Prototype\GAS\CombatAbilityBase.cpp:25)
UE4Editor-Tosh_Prototype.dll!UCombatAbilityBase::HandleEvent(FGameplayTag EventTag, FGameplayEventData EventData) Line 159 (e:\Projects\Gamedev\Tosh_Prototype\Source\Tosh_Prototype\GAS\CombatAbilityBase.cpp:159)
UE4Editor-Tosh_Prototype.dll!USimpleMontageAbilityBase::execHandleEvent(UObject * Context, FFrame & Stack, void * const Z_Param__Result) Line 39 (e:\Projects\Gamedev\Tosh_Prototype\Intermediate\Build\Win64\UE4Editor\Inc\Tosh_Prototype\SimpleMontageAbilityBase.gen.cpp:39)
UE4Editor-CoreUObject.dll!UFunction::Invoke(UObject * Obj, FFrame & Stack, void * const Z_Param__Result) Line 5588 (d:\Build\++UE4\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\Class.cpp:5588)
UE4Editor-CoreUObject.dll!UObject::ProcessEvent(UFunction * Function, void * Parms) Line 1985 (d:\Build\++UE4\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\ScriptCore.cpp:1985)
[Inline Frame] UE4Editor-Tosh_Prototype.dll!TScriptDelegate<FWeakObjectPtr>::ProcessDelegate(void *) Line 247 (e:\Program Files\Epic Games\UE_4.26\Engine\Source\Runtime\Core\Public\UObject\ScriptDelegates.h:247)
UE4Editor-Tosh_Prototype.dll!TMulticastScriptDelegate<FWeakObjectPtr>::ProcessMulticastDelegate<UObject>(void * Parameters) Line 488 (e:\Program Files\Epic Games\UE_4.26\Engine\Source\Runtime\Core\Public\UObject\ScriptDelegates.h:488)
[Inline Frame] UE4Editor-Tosh_Prototype.dll!FToshPlayMontageAndWaitForEventDelegate_DelegateWrapper(const TMulticastScriptDelegate<FWeakObjectPtr> &) Line 12 (e:\Projects\Gamedev\Tosh_Prototype\Source\Tosh_Prototype\GAS\Tosh_PlayMontageAndWaitForEvent.h:12)
UE4Editor-Tosh_Prototype.dll!FToshPlayMontageAndWaitForEventDelegate::Broadcast(FGameplayTag InParam1, FGameplayEventData InParam2) Line 12 (e:\Projects\Gamedev\Tosh_Prototype\Source\Tosh_Prototype\GAS\Tosh_PlayMontageAndWaitForEvent.h:12)
UE4Editor-Tosh_Prototype.dll!UToshAbilityTask_PlayMontageAndWaitForEvent::OnGameplayEvent(FGameplayTag EventTag, const FGameplayEventData * Payload) Line 92 (e:\Projects\Gamedev\Tosh_Prototype\Source\Tosh_Prototype\GAS\Tosh_PlayMontageAndWaitForEvent.cpp:92)
[Inline Frame] UE4Editor-Tosh_Prototype.dll!Invoke(void(UToshAbilityTask_PlayMontageAndWaitForEvent::*)(FGameplayTag, const FGameplayEventData *)) Line 65 (e:\Program Files\Epic Games\UE_4.26\Engine\Source\Runtime\Core\Public\Templates\Invoke.h:65)
[Inline Frame] UE4Editor-Tosh_Prototype.dll!UE4Tuple_Private::TTupleBase<TIntegerSequence<unsigned int> >::ApplyAfter(void(UToshAbilityTask_PlayMontageAndWaitForEvent::*)(FGameplayTag, const FGameplayEventData *) &) Line 299 (e:\Program Files\Epic Games\UE_4.26\Engine\Source\Runtime\Core\Public\Templates\Tuple.h:299)
UE4Editor-Tosh_Prototype.dll!TBaseUObjectMethodDelegateInstance<0,UToshAbilityTask_PlayMontageAndWaitForEvent,void __cdecl(FGameplayTag,FGameplayEventData const *),FDefaultDelegateUserPolicy>::ExecuteIfSafe(FGameplayTag <Params_0>, const FGameplayEventData * <Params_1>) Line 611 (e:\Program Files\Epic Games\UE_4.26\Engine\Source\Runtime\Core\Public\Delegates\DelegateInstancesImpl.h:611)
[Inline Frame] UE4Editor-GameplayAbilities.dll!TMulticastDelegate<void __cdecl(FGameplayTag,FGameplayEventData const *),FDefaultDelegateUserPolicy>::Broadcast(FGameplayTag) Line 955 (d:\Build\++UE4\Sync\Engine\Source\Runtime\Core\Public\Delegates\DelegateSignatureImpl.inl:955)
UE4Editor-GameplayAbilities.dll!UAbilitySystemComponent::HandleGameplayEvent(FGameplayTag EventTag, const FGameplayEventData * Payload) Line 1993 (d:\Build\++UE4\Sync\Engine\Plugins\Runtime\GameplayAbilities\Source\GameplayAbilities\Private\AbilitySystemComponent_Abilities.cpp:1993)
UE4Editor-GameplayAbilities.dll!UAbilitySystemBlueprintLibrary::SendGameplayEventToActor(AActor * Actor, FGameplayTag EventTag, FGameplayEventData Payload) Line 31 (d:\Build\++UE4\Sync\Engine\Plugins\Runtime\GameplayAbilities\Source\GameplayAbilities\Private\AbilitySystemBlueprintLibrary.cpp:31)

Am I doing something wrong?

1 post - 1 participant