Skip to content

Commit 7b2cf7c

Browse files
committed
Store/Create Bond as a shared_ptr to allow shared_from_this()
In order for Bond to inherit from std::enable_shared_from_this to allow its lifetime to be managed to avoid a use after free where its subcriber callback is called before it has been cleaned up from internal rclcpp mechanism. This is being explored either by modification to rclcpp or using a lambda that protects against the the member function going out of scope by testing a weak_ptr to shared_from_this for expiry before the member function bondStatusCB is called. Signed-off-by: Mike Wake <[email protected]>
1 parent eb0d3f4 commit 7b2cf7c

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

nav2_util/include/nav2_util/lifecycle_node.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ class LifecycleNode : public rclcpp_lifecycle::LifecycleNode
210210
void runCleanups();
211211

212212
// Connection to tell that server is still up
213-
std::unique_ptr<bond::Bond> bond_{nullptr};
213+
std::shared_ptr<bond::Bond> bond_{nullptr};
214214
double bond_heartbeat_period;
215215
rclcpp::TimerBase::SharedPtr autostart_timer_;
216216
};

nav2_util/src/lifecycle_node.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void LifecycleNode::createBond()
7373
if (bond_heartbeat_period > 0.0) {
7474
RCLCPP_INFO(get_logger(), "Creating bond (%s) to lifecycle manager.", this->get_name());
7575

76-
bond_ = std::make_unique<bond::Bond>(
76+
bond_ = std::make_shared<bond::Bond>(
7777
std::string("bond"),
7878
this->get_name(),
7979
shared_from_this());

0 commit comments

Comments
 (0)