From f161095c256557f1bd572a7f3c56f1b00c6145fe Mon Sep 17 00:00:00 2001 From: Koichi Imai <45482193+Koichi98@users.noreply.github.com> Date: Fri, 24 Jan 2025 16:36:46 +0900 Subject: [PATCH] fix: decrement borrowed_publisher_num before ros2 publish (#334) fix/dec_brw_pub_num_earlier Signed-off-by: Koichi98 --- src/agnocastlib/include/agnocast_publisher.hpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/agnocastlib/include/agnocast_publisher.hpp b/src/agnocastlib/include/agnocast_publisher.hpp index 1dc9bd8e..e2d82e5d 100644 --- a/src/agnocastlib/include/agnocast_publisher.hpp +++ b/src/agnocastlib/include/agnocast_publisher.hpp @@ -97,14 +97,18 @@ class Publisher exit(EXIT_FAILURE); } + publish_core(topic_name_, publisher_pid_, message.get_timestamp(), opened_mqs_); + // We need to decrement borrowed_publisher_num before ros2_publish, otherwise the buffers used + // for ROS2 serialization will also use shared memory. Since we don't need to store any + // additional data in shared memory after the agnocast publish operation, here is the ideal + // point to decrement. + decrement_borrowed_publisher_num(); + if (do_always_ros2_publish_ || ros2_publisher_->get_subscription_count() > 0) { const MessageT * raw = message.get(); ros2_publisher_->publish(*raw); } - - publish_core(topic_name_, publisher_pid_, message.get_timestamp(), opened_mqs_); message.reset(); - decrement_borrowed_publisher_num(); } uint32_t get_subscription_count() const