1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Timer: Prevent memory leaks when using callAfterDelay

This commit is contained in:
Anthony Nicholls 2023-12-21 21:17:42 +00:00
parent 6056c686b8
commit db60c1d226

View file

@ -344,18 +344,24 @@ void JUCE_CALLTYPE Timer::callPendingTimersSynchronously()
(*instance)->callTimersSynchronously();
}
struct LambdaInvoker final : private Timer
struct LambdaInvoker final : private Timer,
private DeletedAtShutdown
{
LambdaInvoker (int milliseconds, std::function<void()> f) : function (f)
LambdaInvoker (int milliseconds, std::function<void()> f)
: function (std::move (f))
{
startTimer (milliseconds);
}
void timerCallback() override
~LambdaInvoker() final
{
auto f = function;
stopTimer();
}
void timerCallback() final
{
NullCheckedInvocation::invoke (function);
delete this;
f();
}
std::function<void()> function;
@ -365,7 +371,7 @@ struct LambdaInvoker final : private Timer
void JUCE_CALLTYPE Timer::callAfterDelay (int milliseconds, std::function<void()> f)
{
new LambdaInvoker (milliseconds, f);
new LambdaInvoker (milliseconds, std::move (f));
}
} // namespace juce