From a72be2bc8c78b266e2841b87ce1691c9b0a9a107 Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Thu, 13 Jul 2023 18:19:34 -0700 Subject: [PATCH] internal update PiperOrigin-RevId: 547982511 --- internal/test/fake_timer.cc | 10 ++++++---- internal/test/fake_timer.h | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/internal/test/fake_timer.cc b/internal/test/fake_timer.cc index 2bbe3b7f..70929e41 100644 --- a/internal/test/fake_timer.cc +++ b/internal/test/fake_timer.cc @@ -15,9 +15,7 @@ #include "internal/test/fake_timer.h" #include -#include // NOLINT #include -#include // NOLINT #include #include "absl/time/clock.h" @@ -62,6 +60,10 @@ void FakeTimer::ClockUpdated() { absl::Time now = clock_->Now(); int64_t duration = absl::ToInt64Milliseconds(now - start_time_); FakeClock* clock = clock_; + // The timer may be released during callback, save period and delay to avoid + // access exception. + int period = period_; + int delay = delay_; if (duration >= delay_ && fired_count_ == 0) { ++fired_count_; @@ -70,12 +72,12 @@ void FakeTimer::ClockUpdated() { } } - if (period_ == 0 || duration < delay_ || + if (period == 0 || duration < delay || ((clock_ != nullptr) && (clock_ != clock))) { return; } - int count = (duration - delay_) / period_; + int count = (duration - delay) / period; int should_fire_count = count - fired_count_ + 1; for (int i = 0; i < should_fire_count; ++i) { ++fired_count_; diff --git a/internal/test/fake_timer.h b/internal/test/fake_timer.h index e803f4af..24a7c15e 100644 --- a/internal/test/fake_timer.h +++ b/internal/test/fake_timer.h @@ -39,7 +39,7 @@ class FakeTimer : public Timer { std::string id_; int delay_ = 0; - int period_ = false; + int period_ = 0; int fired_count_ = 0; absl::Time start_time_; bool is_started_ = false;