From 211add4fd3b3fb846aeee975dbc6a3158996d839 Mon Sep 17 00:00:00 2001 From: Kai-Tao Xie Date: Mon, 16 Aug 2021 20:18:57 +0800 Subject: [PATCH] fix data race: addPlugin() and resizeMap() can be executed concurrently --- nav2_costmap_2d/include/nav2_costmap_2d/layered_costmap.hpp | 6 ++---- nav2_costmap_2d/src/layered_costmap.cpp | 6 ++++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/nav2_costmap_2d/include/nav2_costmap_2d/layered_costmap.hpp b/nav2_costmap_2d/include/nav2_costmap_2d/layered_costmap.hpp index a0602b6865a..ddf64ffbdc1 100644 --- a/nav2_costmap_2d/include/nav2_costmap_2d/layered_costmap.hpp +++ b/nav2_costmap_2d/include/nav2_costmap_2d/layered_costmap.hpp @@ -146,10 +146,8 @@ class LayeredCostmap /** * @brief Add a new plugin to the plugins vector to process */ - void addPlugin(std::shared_ptr plugin) - { - plugins_.push_back(plugin); - } + void addPlugin(std::shared_ptr plugin); + /** * @brief Add a new costmap filter plugin to the filters vector to process diff --git a/nav2_costmap_2d/src/layered_costmap.cpp b/nav2_costmap_2d/src/layered_costmap.cpp index e31ebe1414c..bd3d37cd31e 100644 --- a/nav2_costmap_2d/src/layered_costmap.cpp +++ b/nav2_costmap_2d/src/layered_costmap.cpp @@ -89,6 +89,12 @@ LayeredCostmap::~LayeredCostmap() } } +void LayeredCostmap::addPlugin(std::shared_ptr plugin) +{ + std::unique_lock lock(*(combined_costmap_.getMutex())); + plugins_.push_back(plugin); +} + void LayeredCostmap::resizeMap( unsigned int size_x, unsigned int size_y, double resolution, double origin_x,