Skip to content

Commit dca853a

Browse files
avalen2022claude
andcommitted
fix(TopicPublisherROS2): prevent crash when enabling Re-Publisher
- GenericPublisher passed its `callbacks_` member by reference to rclcpp::PublisherBase. Base init runs before member init, so rclcpp read uninitialized std::function memory and crashed inside the PublisherBase ctor. Pass a temporary rclcpp::PublisherEventCallbacks{} and drop the dead member. - setEnabled() eagerly created tf2_ros TransformBroadcaster / Static broadcaster even for bags without /tf data. On Jazzy/Rolling this triggers a separate tf2_ros QoS-override parameter-declaration crash inside rclcpp. Defer broadcaster creation to broadcastTF() so it only happens once /tf or /tf_static actually carries data. Closes #477, #759. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 9cb0b59 commit dca853a

2 files changed

Lines changed: 10 additions & 13 deletions

File tree

src/TopicPublisherROS2/generic_publisher.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class GenericPublisher : public rclcpp::PublisherBase
3030
const rosidl_message_type_support_t& type_support)
3131
// The rclcpp::PublisherBase constructor grew event-callback parameters after Humble (rclcpp 16.x).
3232
#if RCLCPP_VERSION_GTE(17, 0, 0)
33-
: rclcpp::PublisherBase(node_base, topic_name, type_support, rcl_publisher_get_default_options(), callbacks_, true)
33+
: rclcpp::PublisherBase(node_base, topic_name, type_support, rcl_publisher_get_default_options(),
34+
rclcpp::PublisherEventCallbacks{}, true)
3435
#else
3536
: rclcpp::PublisherBase(node_base, topic_name, type_support, rcl_publisher_get_default_options())
3637
#endif
@@ -58,9 +59,6 @@ class GenericPublisher : public rclcpp::PublisherBase
5859
return std::make_shared<GenericPublisher>(node.get_node_base_interface().get(), topic_name, *type_support);
5960
}
6061

61-
#if RCLCPP_VERSION_GTE(17, 0, 0)
62-
rclcpp::PublisherEventCallbacks callbacks_;
63-
#endif
6462
};
6563

6664
#endif // GENERIC_PUBLISHER_H

src/TopicPublisherROS2/publisher_ros2.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,6 @@ void TopicPublisherROS2::setEnabled(bool to_enable)
111111

112112
updatePublishers();
113113

114-
if (!_tf_broadcaster)
115-
{
116-
_tf_broadcaster = std::make_unique<tf2_ros::TransformBroadcaster>(*_node);
117-
}
118-
if (!_tf_static_broadcaster)
119-
{
120-
_tf_static_broadcaster = std::make_unique<tf2_ros::StaticTransformBroadcaster>(*_node);
121-
}
122-
123114
_previous_play_index = std::numeric_limits<int>::max();
124115
}
125116
else
@@ -293,10 +284,18 @@ void TopicPublisherROS2::broadcastTF(double current_time)
293284
}
294285
if (transforms_ptr == &transforms)
295286
{
287+
if (!_tf_broadcaster)
288+
{
289+
_tf_broadcaster = std::make_shared<tf2_ros::TransformBroadcaster>(*_node);
290+
}
296291
_tf_broadcaster->sendTransform(transforms_vector);
297292
}
298293
else
299294
{
295+
if (!_tf_static_broadcaster)
296+
{
297+
_tf_static_broadcaster = std::make_shared<tf2_ros::StaticTransformBroadcaster>(*_node);
298+
}
300299
_tf_static_broadcaster->sendTransform(transforms_vector);
301300
}
302301
}

0 commit comments

Comments
 (0)