Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(autoware_universe_utils): fix memory leak of time_keeper #1456

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -94,9 +94,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::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 @@ -81,7 +81,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 @@ -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();
Expand Down
Loading