From dca853a754760507832c4a4fa1f18ba38212c79f Mon Sep 17 00:00:00 2001 From: avalen2022 Date: Thu, 23 Apr 2026 11:38:51 +0200 Subject: [PATCH 1/2] 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) --- src/TopicPublisherROS2/generic_publisher.h | 6 ++---- src/TopicPublisherROS2/publisher_ros2.cpp | 17 ++++++++--------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/TopicPublisherROS2/generic_publisher.h b/src/TopicPublisherROS2/generic_publisher.h index eb231cc..0ed718d 100644 --- a/src/TopicPublisherROS2/generic_publisher.h +++ b/src/TopicPublisherROS2/generic_publisher.h @@ -30,7 +30,8 @@ class GenericPublisher : public rclcpp::PublisherBase const rosidl_message_type_support_t& type_support) // The rclcpp::PublisherBase constructor grew event-callback parameters after Humble (rclcpp 16.x). #if RCLCPP_VERSION_GTE(17, 0, 0) - : rclcpp::PublisherBase(node_base, topic_name, type_support, rcl_publisher_get_default_options(), callbacks_, true) + : rclcpp::PublisherBase(node_base, topic_name, type_support, rcl_publisher_get_default_options(), + rclcpp::PublisherEventCallbacks{}, true) #else : rclcpp::PublisherBase(node_base, topic_name, type_support, rcl_publisher_get_default_options()) #endif @@ -58,9 +59,6 @@ class GenericPublisher : public rclcpp::PublisherBase return std::make_shared(node.get_node_base_interface().get(), topic_name, *type_support); } -#if RCLCPP_VERSION_GTE(17, 0, 0) - rclcpp::PublisherEventCallbacks callbacks_; -#endif }; #endif // GENERIC_PUBLISHER_H diff --git a/src/TopicPublisherROS2/publisher_ros2.cpp b/src/TopicPublisherROS2/publisher_ros2.cpp index 7126d2d..375536b 100644 --- a/src/TopicPublisherROS2/publisher_ros2.cpp +++ b/src/TopicPublisherROS2/publisher_ros2.cpp @@ -111,15 +111,6 @@ void TopicPublisherROS2::setEnabled(bool to_enable) updatePublishers(); - if (!_tf_broadcaster) - { - _tf_broadcaster = std::make_unique(*_node); - } - if (!_tf_static_broadcaster) - { - _tf_static_broadcaster = std::make_unique(*_node); - } - _previous_play_index = std::numeric_limits::max(); } else @@ -293,10 +284,18 @@ void TopicPublisherROS2::broadcastTF(double current_time) } if (transforms_ptr == &transforms) { + if (!_tf_broadcaster) + { + _tf_broadcaster = std::make_shared(*_node); + } _tf_broadcaster->sendTransform(transforms_vector); } else { + if (!_tf_static_broadcaster) + { + _tf_static_broadcaster = std::make_shared(*_node); + } _tf_static_broadcaster->sendTransform(transforms_vector); } } From 3a2b727c3654d426d970dc031659dd5d023e1e7c Mon Sep 17 00:00:00 2001 From: avalen2022 Date: Fri, 24 Apr 2026 13:50:07 +0200 Subject: [PATCH 2/2] style: clang-format generic_publisher.h --- src/TopicPublisherROS2/generic_publisher.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/TopicPublisherROS2/generic_publisher.h b/src/TopicPublisherROS2/generic_publisher.h index 0ed718d..4e3cf57 100644 --- a/src/TopicPublisherROS2/generic_publisher.h +++ b/src/TopicPublisherROS2/generic_publisher.h @@ -58,7 +58,6 @@ class GenericPublisher : public rclcpp::PublisherBase return std::make_shared(node.get_node_base_interface().get(), topic_name, *type_support); } - }; #endif // GENERIC_PUBLISHER_H