From 9d3274d0418d8766ee60112eba702da917176368 Mon Sep 17 00:00:00 2001 From: Yadunund Date: Fri, 6 Sep 2024 15:36:08 +0800 Subject: [PATCH] Use count to check if node exists Signed-off-by: Yadunund --- rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp b/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp index 0586bcc9..ed4a88e9 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp +++ b/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp @@ -452,8 +452,7 @@ bool rmw_context_impl_s::create_node_data( const std::string & node_name) { std::lock_guard lock(data_->mutex_); - auto node_insertion = data_->nodes_.insert(std::make_pair(node, nullptr)); - if (!node_insertion.second) { + if (data_->nodes_.count(node) > 0) { // Node already exists. return false; } @@ -478,7 +477,10 @@ bool rmw_context_impl_s::create_node_data( return false; } - node_insertion.first->second = std::move(node_data); + auto node_insertion = data_->nodes_.insert(std::make_pair(node, std::move(node_data))); + if (!node_insertion.second) { + return false; + } return true; }