diff --git a/include/behaviortree_cpp/blackboard.h b/include/behaviortree_cpp/blackboard.h index 436542bf0..e12e9d984 100644 --- a/include/behaviortree_cpp/blackboard.h +++ b/include/behaviortree_cpp/blackboard.h @@ -130,7 +130,7 @@ class Blackboard void debugMessage() const; - [[nodiscard]] std::vector getKeys() const; + [[nodiscard]] std::vector getKeys() const; [[deprecated("This command is unsafe. Consider using Backup/Restore instead")]] void clear(); diff --git a/src/blackboard.cpp b/src/blackboard.cpp index 305fdde6d..caf9e7040 100644 --- a/src/blackboard.cpp +++ b/src/blackboard.cpp @@ -113,13 +113,14 @@ void Blackboard::debugMessage() const } } -std::vector Blackboard::getKeys() const +std::vector Blackboard::getKeys() const { + std::unique_lock lock(mutex_); if(storage_.empty()) { return {}; } - std::vector out; + std::vector out; out.reserve(storage_.size()); for(const auto& entry_it : storage_) { @@ -272,10 +273,9 @@ std::shared_ptr Blackboard::createEntryImpl(const std::string nlohmann::json ExportBlackboardToJSON(const Blackboard& blackboard) { nlohmann::json dest; - for(auto entry_name : blackboard.getKeys()) + for(const std::string& name : blackboard.getKeys()) { - std::string name(entry_name); - if(auto any_ref = blackboard.getAnyLocked(name)) + if(AnyPtrLocked any_ref = blackboard.getAnyLocked(name)) { if(auto any_ptr = any_ref.get()) { diff --git a/tests/gtest_blackboard.cpp b/tests/gtest_blackboard.cpp index 35be37dab..676267de3 100644 --- a/tests/gtest_blackboard.cpp +++ b/tests/gtest_blackboard.cpp @@ -558,9 +558,9 @@ TEST(BlackboardTest, BlackboardBackup) for(const auto& sub : tree.subtrees) { std::vector keys; - for(const auto& str_view : sub->blackboard->getKeys()) + for(const std::string& str : sub->blackboard->getKeys()) { - keys.push_back(std::string(str_view)); + keys.push_back(str); } expected_keys.push_back(keys); }