From 1b60c93b6aad789023ce22ebb41d5a6d4c79b4f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Gonz=C3=A1lez?= Date: Fri, 13 Aug 2021 09:45:47 +0200 Subject: [PATCH] Fix wrong iterator after vector reallocation in LivelinessManager (#2141) 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) --- 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();