Skip to content

Commit 284ed1d

Browse files
committed
bug fix (segfault when creating multiple trees)
1 parent c51b605 commit 284ed1d

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

behaviortree_ros2/include/behaviortree_ros2/bt_topic_sub_node.hpp

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,31 @@ class RosTopicSubNode : public BT::ConditionNode
5252
protected:
5353
struct SubscriberInstance
5454
{
55+
void init(std::shared_ptr<rclcpp::Node> node, const std::string& topic_name)
56+
{
57+
// create a callback group for this particular instance
58+
callback_group =
59+
node->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive, false);
60+
callback_group_executor.add_callback_group(
61+
callback_group, node->get_node_base_interface());
62+
63+
rclcpp::SubscriptionOptions option;
64+
option.callback_group = callback_group;
65+
66+
// The callback will broadcast to all the instances of RosTopicSubNode<T>
67+
auto callback = [this](const std::shared_ptr<TopicT> msg)
68+
{
69+
broadcaster(msg);
70+
};
71+
subscriber = node->create_subscription<TopicT>(topic_name, 1, callback, option);
72+
}
73+
5574
std::shared_ptr<Subscriber> subscriber;
5675
rclcpp::CallbackGroup::SharedPtr callback_group;
5776
rclcpp::executors::SingleThreadedExecutor callback_group_executor;
58-
boost::signals2::signal<void (const std::shared_ptr<TopicT>&)> broadcaster;
77+
boost::signals2::signal<void (const std::shared_ptr<TopicT>)> broadcaster;
78+
79+
5980
};
6081

6182
static std::mutex& registryMutex()
@@ -75,6 +96,7 @@ class RosTopicSubNode : public BT::ConditionNode
7596
SubscriberInstance* sub_instance_ = nullptr;
7697
std::shared_ptr<TopicT> last_msg_;
7798
std::string topic_name_;
99+
boost::signals2::connection signal_connection_;
78100

79101
rclcpp::Logger logger()
80102
{
@@ -94,7 +116,10 @@ class RosTopicSubNode : public BT::ConditionNode
94116
const BT::NodeConfig& conf,
95117
const RosNodeParams& params);
96118

97-
virtual ~RosTopicSubNode() = default;
119+
virtual ~RosTopicSubNode()
120+
{
121+
signal_connection_.disconnect();
122+
}
98123

99124
/**
100125
* @brief Any subclass of RosTopicNode that accepts parameters must provide a
@@ -129,7 +154,7 @@ class RosTopicSubNode : public BT::ConditionNode
129154
*
130155
* @return the new status of the Node, based on last_msg
131156
*/
132-
virtual NodeStatus onTick(const std::shared_ptr<TopicT>& last_msg) = 0;
157+
virtual NodeStatus onTick(const std::shared_ptr<TopicT> last_msg) = 0;
133158

134159
private:
135160

@@ -206,33 +231,17 @@ template<class T> inline
206231
// just created (subscriber is not initialized)
207232
if(!sub_instance_->subscriber)
208233
{
209-
// create a callback group for this particular instance
210-
sub_instance_->callback_group =
211-
node_->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive, false);
212-
sub_instance_->callback_group_executor.add_callback_group(
213-
sub_instance_->callback_group, node_->get_node_base_interface());
214-
215-
rclcpp::SubscriptionOptions sub_option;
216-
sub_option.callback_group = sub_instance_->callback_group;
217-
218-
// The callback will broadcast to all the instances of RosTopicSubNode<T>
219-
auto callback = [this](const std::shared_ptr<T> msg) {
220-
sub_instance_->broadcaster(msg);
221-
};
222-
sub_instance_->subscriber =
223-
node_->create_subscription<T>(topic_name, 1, callback, sub_option);
234+
sub_instance_->init(node_, topic_name);
224235

225236
RCLCPP_INFO(logger(),
226237
"Node [%s] created Subscriber to topic [%s]",
227238
name().c_str(), topic_name.c_str() );
228239
}
229240

230241
// add "this" as received of the broadcaster
231-
sub_instance_->broadcaster.connect([this](const std::shared_ptr<T> msg)
232-
{
233-
last_msg_ = msg;
234-
});
235-
242+
signal_connection_ = sub_instance_->broadcaster.connect(
243+
[this](const std::shared_ptr<T> msg) { last_msg_ = msg; } );
244+
236245
topic_name_ = topic_name;
237246
return true;
238247
}

0 commit comments

Comments
 (0)