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:
parent
6056c686b8
commit
db60c1d226
1 changed files with 12 additions and 6 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue