Skip to content

Commit 13ac473

Browse files
authored
fix(autoware_universe_utils): fix memory leak of time_keeper (#8425)
fix bug of time_keeper Signed-off-by: Y.Hisaki <yhisaki31@gmail.com>
1 parent 9c95d21 commit 13ac473

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

Diff for: common/autoware_universe_utils/include/autoware/universe_utils/system/time_keeper.hpp

+6-6
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
@@ -101,10 +101,10 @@ class ProcessingTimeNode : public std::enable_shared_from_this<ProcessingTimeNod
101101
std::string get_name() const;
102102

103103
private:
104-
const std::string name_; //!< Name of the node
105-
double processing_time_{0.0}; //!< Processing time of the node
106-
std::string comment_; //!< Comment for the node
107-
std::shared_ptr<ProcessingTimeNode> parent_node_{nullptr}; //!< Shared pointer to the parent node
104+
const std::string name_; //!< Name of the node
105+
double processing_time_{0.0}; //!< Processing time of the node
106+
std::string comment_; //!< Comment for the node
107+
std::weak_ptr<ProcessingTimeNode> parent_node_; //!< Weak pointer to the parent node
108108
std::vector<std::shared_ptr<ProcessingTimeNode>>
109109
child_nodes_; //!< Vector of shared pointers to the child nodes
110110
};

Diff for: 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
}
@@ -86,7 +86,7 @@ tier4_debug_msgs::msg::ProcessingTimeTree ProcessingTimeNode::to_msg() const
8686
return time_tree_msg;
8787
}
8888

89-
std::shared_ptr<ProcessingTimeNode> ProcessingTimeNode::get_parent_node() const
89+
std::weak_ptr<ProcessingTimeNode> ProcessingTimeNode::get_parent_node() const
9090
{
9191
return parent_node_;
9292
}
@@ -152,7 +152,7 @@ void TimeKeeper::end_track(const std::string & func_name)
152152
}
153153
const double processing_time = stop_watch_.toc(func_name);
154154
current_time_node_->set_time(processing_time);
155-
current_time_node_ = current_time_node_->get_parent_node();
155+
current_time_node_ = current_time_node_->get_parent_node().lock();
156156

157157
if (current_time_node_ == nullptr) {
158158
report();

0 commit comments

Comments
 (0)