From 8e5259b1501e2367d2234f7b2040c94ec7d82647 Mon Sep 17 00:00:00 2001 From: Michael Carroll Date: Sat, 28 Nov 2026 11:09:59 -0600 Subject: [PATCH] test_qos_event: declare event counters before the entities that call them test_on_new_event_callback installed callbacks capturing local atomics by reference on a publisher/subscription whose deadline events fire every few milliseconds on the middleware's thread. The counters were declared after the publisher and subscription, so they were destroyed first and the still-running callbacks wrote to dead stack memory (AddressSanitizer: stack-use-after-scope). Declare them before the entities. Signed-off-by: Michael Carroll --- rclcpp/test/rclcpp/test_qos_event.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/rclcpp/test/rclcpp/test_qos_event.cpp b/rclcpp/test/rclcpp/test_qos_event.cpp index f14cd5878d..d4c3e9417a 100644 --- a/rclcpp/test/rclcpp/test_qos_event.cpp +++ b/rclcpp/test/rclcpp/test_qos_event.cpp @@ -365,6 +365,11 @@ TEST_F(TestQosEvent, test_on_new_event_callback) auto offered_deadline = rclcpp::Duration(std::chrono::milliseconds(1)); auto requested_deadline = rclcpp::Duration(std::chrono::milliseconds(2)); + // The event callbacks keep firing on the middleware's thread until the publisher and subscription + // are destroyed, so the counters they capture must outlive both. + std::atomic c1 {0}; + std::atomic c2 {0}; + rclcpp::QoS qos_profile_publisher(10); qos_profile_publisher.deadline(offered_deadline); rclcpp::PublisherOptions pub_options; @@ -379,7 +384,6 @@ TEST_F(TestQosEvent, test_on_new_event_callback) auto subscription = node->create_subscription( topic_name, qos_profile_subscription, message_callback, sub_options); - std::atomic c1 {0}; auto increase_c1_cb = [&c1](size_t count_events) {c1 += count_events;}; publisher->set_on_new_qos_event_callback(increase_c1_cb, RCL_PUBLISHER_OFFERED_DEADLINE_MISSED); @@ -392,7 +396,6 @@ TEST_F(TestQosEvent, test_on_new_event_callback) EXPECT_GT(c1, 1u); - std::atomic c2 {0}; auto increase_c2_cb = [&c2](size_t count_events) {c2 += count_events;}; subscription->set_on_new_qos_event_callback( increase_c2_cb,