Skip to content

Commit

Permalink
fix(autoware_universe_utils): fix memory leak of time_keeper (#8425)
Browse files Browse the repository at this point in the history
fix bug of time_keeper

Signed-off-by: Y.Hisaki <[email protected]>
  • Loading branch information
yhisaki authored and technolojin committed Aug 13, 2024
1 parent f04f796 commit 17347bb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ class ProcessingTimeNode : public std::enable_shared_from_this<ProcessingTimeNod
/**
* @brief Get the parent node
*
* @return std::shared_ptr<ProcessingTimeNode> Shared pointer to the parent node
* @return std::weak_ptr<ProcessingTimeNode> Weak pointer to the parent node
*/
std::shared_ptr<ProcessingTimeNode> get_parent_node() const;
std::weak_ptr<ProcessingTimeNode> get_parent_node() const;

/**
* @brief Get the child nodes
Expand Down Expand Up @@ -101,10 +101,10 @@ class ProcessingTimeNode : public std::enable_shared_from_this<ProcessingTimeNod
std::string get_name() const;

private:
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::shared_ptr<ProcessingTimeNode> 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<ProcessingTimeNode> parent_node_; //!< Weak pointer to the parent node
std::vector<std::shared_ptr<ProcessingTimeNode>>
child_nodes_; //!< Vector of shared pointers to the child nodes
};
Expand Down
6 changes: 3 additions & 3 deletions common/autoware_universe_utils/src/system/time_keeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ProcessingTimeNode::ProcessingTimeNode(const std::string & name) : name_(name)
std::shared_ptr<ProcessingTimeNode> ProcessingTimeNode::add_child(const std::string & name)
{
auto new_child_node = std::make_shared<ProcessingTimeNode>(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;
}
Expand Down Expand Up @@ -86,7 +86,7 @@ tier4_debug_msgs::msg::ProcessingTimeTree ProcessingTimeNode::to_msg() const
return time_tree_msg;
}

std::shared_ptr<ProcessingTimeNode> ProcessingTimeNode::get_parent_node() const
std::weak_ptr<ProcessingTimeNode> ProcessingTimeNode::get_parent_node() const
{
return parent_node_;
}
Expand Down Expand Up @@ -152,7 +152,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();
Expand Down

0 comments on commit 17347bb

Please sign in to comment.