Skip to content

Commit 91d0e85

Browse files
authored
fix(autoware_universe_utils): fix memory leak of time_keeper (#1456)
fix bug of time_keeper Signed-off-by: Y.Hisaki <yhisaki31@gmail.com>
1 parent 49231bc commit 91d0e85

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

common/autoware_universe_utils/include/autoware/universe_utils/system/time_keeper.hpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ class ProcessingTimeNode : public std::enable_shared_from_this<ProcessingTimeNod
6767
/**
6868
* @brief Get the parent node
6969
*
70-
* @return std::shared_ptr<ProcessingTimeNode> Shared pointer to the parent node
70+
* @return std::weak_ptr<ProcessingTimeNode> Weak pointer to the parent node
7171
*/
72-
std::shared_ptr<ProcessingTimeNode> get_parent_node() const;
72+
std::weak_ptr<ProcessingTimeNode> get_parent_node() const;
7373

7474
/**
7575
* @brief Get the child nodes
@@ -94,9 +94,10 @@ class ProcessingTimeNode : public std::enable_shared_from_this<ProcessingTimeNod
9494
std::string get_name() const;
9595

9696
private:
97-
const std::string name_; //!< Name of the node
98-
double processing_time_{0.0}; //!< Processing time of the node
99-
std::shared_ptr<ProcessingTimeNode> parent_node_{nullptr}; //!< Shared pointer to the parent node
97+
const std::string name_; //!< Name of the node
98+
double processing_time_{0.0}; //!< Processing time of the node
99+
std::string comment_; //!< Comment for the node
100+
std::weak_ptr<ProcessingTimeNode> parent_node_; //!< Weak pointer to the parent node
100101
std::vector<std::shared_ptr<ProcessingTimeNode>>
101102
child_nodes_; //!< Vector of shared pointers to the child nodes
102103
};

common/autoware_universe_utils/src/system/time_keeper.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ ProcessingTimeNode::ProcessingTimeNode(const std::string & name) : name_(name)
2828
std::shared_ptr<ProcessingTimeNode> ProcessingTimeNode::add_child(const std::string & name)
2929
{
3030
auto new_child_node = std::make_shared<ProcessingTimeNode>(name);
31-
new_child_node->parent_node_ = shared_from_this();
31+
new_child_node->parent_node_ = weak_from_this();
3232
child_nodes_.push_back(new_child_node);
3333
return new_child_node;
3434
}
@@ -81,7 +81,7 @@ tier4_debug_msgs::msg::ProcessingTimeTree ProcessingTimeNode::to_msg() const
8181
return time_tree_msg;
8282
}
8383

84-
std::shared_ptr<ProcessingTimeNode> ProcessingTimeNode::get_parent_node() const
84+
std::weak_ptr<ProcessingTimeNode> ProcessingTimeNode::get_parent_node() const
8585
{
8686
return parent_node_;
8787
}
@@ -133,7 +133,7 @@ void TimeKeeper::end_track(const std::string & func_name)
133133
}
134134
const double processing_time = stop_watch_.toc(func_name);
135135
current_time_node_->set_time(processing_time);
136-
current_time_node_ = current_time_node_->get_parent_node();
136+
current_time_node_ = current_time_node_->get_parent_node().lock();
137137

138138
if (current_time_node_ == nullptr) {
139139
report();

0 commit comments

Comments
 (0)