From d8c2e0a37a9b5070f1c434c49d0241f24a3c9a72 Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Mon, 26 Apr 2021 16:23:05 +0200 Subject: [PATCH] fix(timer) correctly handle deleting a timer in an other timer with repeat_count = 1 --- src/misc/lv_timer.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/misc/lv_timer.c b/src/misc/lv_timer.c index b751c6284..8602ce5ca 100644 --- a/src/misc/lv_timer.c +++ b/src/misc/lv_timer.c @@ -283,19 +283,27 @@ static bool lv_timer_exec(lv_timer_t * timer) { if(timer->paused) return false; + if(timer->repeat_count == 0) { + TIMER_TRACE("deleting timer with %p callback because the repeat count is over", timer->timer_cb); + lv_timer_del(timer); + return false; + } + bool exec = false; if(lv_timer_time_remaining(timer) == 0) { + /* Decrement the repeat count before executing the timer_cb. + * If any timer is deleted `if(timer->repeat_count == 0)` is not executed below + * but at least the repeat count is zero and the timer can be deleted in the next round*/ + int32_t original_repeat_count = timer->repeat_count; + if(timer->repeat_count > 0) timer->repeat_count--; timer->last_run = lv_tick_get(); TIMER_TRACE("calling timer callback: %p", timer->timer_cb); - if(timer->timer_cb) timer->timer_cb(timer); + if(timer->timer_cb && original_repeat_count != 0) timer->timer_cb(timer); TIMER_TRACE("timer callback %p finished", timer->timer_cb); LV_ASSERT_MEM_INTEGRITY(); /*Delete if it was a one shot lv_timer*/ if(timer_deleted == false) { /*The timer might be deleted by itself as well*/ - if(timer->repeat_count > 0) { - timer->repeat_count--; - } if(timer->repeat_count == 0) { TIMER_TRACE("deleting timer with %p callback because the repeat count is over", timer->timer_cb); lv_timer_del(timer);