Should I clear timers at the end?

Hi,

I start a few timers in one of AActor in PostInitializeComponents method. Do I need to clear them in the destructor or in another method? I guess they significantly affect performance, so I don’t want the timers still tick for the object which is not used anymore.

Thanks!

Hello, yateam

You should clear active timers when destroying/deactivating objects.
Depending on the situation, you may want to clear one timer or all of them:

//Clear the custom timer
GetWorld()->GetTimerManager().ClearTimer(CustomTimerHandle);
//Clear all timers for the object
GetWorld()->GetTimerManager().ClearAllTimersForObject(this);

AActor::EndPlay method would be a good place to clear your timers.

Hope this helped!

Have a great day!

Thank you!

Hi, I have a question about ClearAllTimersForObject.

Inside one of my classes I have the following piece of code:

auto MyLambda = [] { //DoSomething };
FTimerDelegate MyDelegate = FTimerDelegate::CreateLambda(MyLambda);
GetWorldTimerManager().SetTimer(ATimerHandleInTheHeader, MyDelegate, ... );

In my EndPlay I do GetWorldTimerManager().ClearAllTimersForObject(this) but the timer with the lambda is still getting called even after the object was destroyed. Is it because my lambda should be a proper function?