From 392172b7c624be148067e164922390aea470d9c4 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 7 Oct 2021 08:55:07 +0200 Subject: [PATCH] Fix wrong iterator after vector reallocation in LivelinessManager (#2147) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Refs #12361. Fix wrong iterator after vector reallocation Signed-off-by: Ricardo González Moreno * Refs #12361. Always return true adding a writer Signed-off-by: Ricardo González Moreno * Update src/cpp/rtps/writer/LivelinessManager.cpp Co-authored-by: Eduardo Ponz Segrelles Co-authored-by: Eduardo Ponz Segrelles (cherry picked from commit cbac2bd3a73f1996fbbcc3be5ffdd00402425e49) Co-authored-by: Ricardo González --- src/cpp/rtps/writer/LivelinessManager.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/cpp/rtps/writer/LivelinessManager.cpp b/src/cpp/rtps/writer/LivelinessManager.cpp index a507776704f..b48c61b53f8 100644 --- a/src/cpp/rtps/writer/LivelinessManager.cpp +++ b/src/cpp/rtps/writer/LivelinessManager.cpp @@ -61,6 +61,19 @@ bool LivelinessManager::add_writer( } } writers_.emplace_back(guid, kind, lease_duration); + + if (!calculate_next()) + { + timer_.cancel_timer(); + return true; + } + + // Some times the interval could be negative if a writer expired during the call to this function + // Once in this situation there is not much we can do but let asio timers expire immediately + auto interval = timer_owner_->time - steady_clock::now(); + timer_.update_interval_millisec((double)duration_cast(interval).count()); + timer_.restart_timer(); + return true; } @@ -95,9 +108,8 @@ bool LivelinessManager::remove_writer( } } - if (timer_owner_ != nullptr && timer_owner_->guid == guid) + if (timer_owner_ != nullptr) { - timer_owner_ = nullptr; if (!calculate_next()) { timer_.cancel_timer();