Safety of using throwaway FTimerHandles

In my project one function needs to call another after a certain delay. One problem is that it’s possible I’ll need to call the first function multiple times even before the first timer executes so if I had only one FTimerHandle, it would be overwritten every time I call the function. Is using a throwaway FTimerHandle safe? Is it possible that it won’t get deleted after the timer execution that would result in a memory leak? Is there a better solution for handling this situation?

Minimal example what I want to make

void MainFunction()
{
   for(int i = 1; i < 10; i++)
   {
      Foo(i);
   }
}


void Foo(float Duration)
{
    FTimerHandle _;
    GetWorld()->GetTimerManager().SetTimer(_, this, &AThisActor::Bar, Duration, false);
}

void Bar()
{
   Do smth;
}

It is generally safe the way you’re using it, the only time it’s not safe is when using a lambda because the timer does not get cleared with the actor being destroyed so need that handle to clear it.

In your example each timer will trigger at the indicated time and is independent.

1 Like

Thanks!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.