From 91d0e856035df0e78cfc8fb23a0fa6e2ef9a98f1 Mon Sep 17 00:00:00 2001 From: Yukinari Hisaki <42021302+yhisaki@users.noreply.github.com> Date: Fri, 9 Aug 2024 18:23:52 +0900 Subject: [PATCH] fix(autoware_universe_utils): fix memory leak of time_keeper (#1456) fix bug of time_keeper Signed-off-by: Y.Hisaki --- .../autoware/universe_utils/system/time_keeper.hpp | 11 ++++++----- .../src/system/time_keeper.cpp | 6 +++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/common/autoware_universe_utils/include/autoware/universe_utils/system/time_keeper.hpp b/common/autoware_universe_utils/include/autoware/universe_utils/system/time_keeper.hpp index c635138416aa5..b6c12714c5daf 100644 --- a/common/autoware_universe_utils/include/autoware/universe_utils/system/time_keeper.hpp +++ b/common/autoware_universe_utils/include/autoware/universe_utils/system/time_keeper.hpp @@ -67,9 +67,9 @@ class ProcessingTimeNode : public std::enable_shared_from_this Shared pointer to the parent node + * @return std::weak_ptr Weak pointer to the parent node */ - std::shared_ptr get_parent_node() const; + std::weak_ptr get_parent_node() const; /** * @brief Get the child nodes @@ -94,9 +94,10 @@ class ProcessingTimeNode : public std::enable_shared_from_this parent_node_{nullptr}; //!< Shared pointer to the parent node + const std::string name_; //!< Name of the node + double processing_time_{0.0}; //!< Processing time of the node + std::string comment_; //!< Comment for the node + std::weak_ptr parent_node_; //!< Weak pointer to the parent node std::vector> child_nodes_; //!< Vector of shared pointers to the child nodes }; diff --git a/common/autoware_universe_utils/src/system/time_keeper.cpp b/common/autoware_universe_utils/src/system/time_keeper.cpp index 429f063dfc62e..fa73c57369d94 100644 --- a/common/autoware_universe_utils/src/system/time_keeper.cpp +++ b/common/autoware_universe_utils/src/system/time_keeper.cpp @@ -28,7 +28,7 @@ ProcessingTimeNode::ProcessingTimeNode(const std::string & name) : name_(name) std::shared_ptr ProcessingTimeNode::add_child(const std::string & name) { auto new_child_node = std::make_shared(name); - new_child_node->parent_node_ = shared_from_this(); + new_child_node->parent_node_ = weak_from_this(); child_nodes_.push_back(new_child_node); return new_child_node; } @@ -81,7 +81,7 @@ tier4_debug_msgs::msg::ProcessingTimeTree ProcessingTimeNode::to_msg() const return time_tree_msg; } -std::shared_ptr ProcessingTimeNode::get_parent_node() const +std::weak_ptr ProcessingTimeNode::get_parent_node() const { return parent_node_; } @@ -133,7 +133,7 @@ void TimeKeeper::end_track(const std::string & func_name) } const double processing_time = stop_watch_.toc(func_name); current_time_node_->set_time(processing_time); - current_time_node_ = current_time_node_->get_parent_node(); + current_time_node_ = current_time_node_->get_parent_node().lock(); if (current_time_node_ == nullptr) { report();