From a9015334eedfde063e070cc7bb63f21c5ac162b0 Mon Sep 17 00:00:00 2001 From: Dmitry Kargin Date: Sat, 30 Nov 2024 18:22:36 +0300 Subject: [PATCH] More checks in Connection class --- include/miniros/internal/observer.h | 10 ++++++++++ src/transport/connection.cpp | 5 +++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/include/miniros/internal/observer.h b/include/miniros/internal/observer.h index ca809bd..efe11da 100644 --- a/include/miniros/internal/observer.h +++ b/include/miniros/internal/observer.h @@ -1,6 +1,7 @@ #ifndef MINIROS_OBSERVER_H #define MINIROS_OBSERVER_H +#include #include // for std::is_base_of namespace miniros { @@ -98,6 +99,15 @@ class Iterator { return *m_obj; } + operator bool () const { + return m_obj != nullptr; + } + + ConnectionImpl* operator -> () { + assert(m_obj); + return m_obj; + } + protected: ConnectionImpl* m_obj; }; diff --git a/src/transport/connection.cpp b/src/transport/connection.cpp index 08d5c9f..f9c2421 100644 --- a/src/transport/connection.cpp +++ b/src/transport/connection.cpp @@ -357,8 +357,9 @@ void Connection::drop(DropReason reason) std::scoped_lock lock(drop_watchers_); for (auto it = drop_watchers_.begin(); it != drop_watchers_.end(); it++) { - DropWatcher& watcher = *it; - watcher.onConnectionDropped(shared_from_this(), reason); + if (!it) + continue; + it->onConnectionDropped(shared_from_this(), reason); } } transport_->close();