From 599b5c91dccd341543a6e62e08a6105cc80408ab Mon Sep 17 00:00:00 2001 From: Functionhx <2994114386@qq.com> Date: Sun, 12 Jul 2026 11:04:36 +0800 Subject: [PATCH] fix: always set history_memory_policy to PREALLOCATED_WITH_REALLOC When data_sharing is AUTOMATIC (default), the history_memory_policy was only set to PREALLOCATED_WITH_REALLOC_MEMORY_MODE inside the block gated by leave_middleware_default_qos. This meant users with default QoS settings could hit NotEnoughMemoryException when the preallocated pool was exhausted, because the policy was left at PREALLOCATED_MEMORY_MODE. Move the assignment outside the conditional so it always applies. Test plan: built and verified NotEnoughMemoryException no longer occurs with default data_sharing AUTOMATIC configuration. Signed-off-by: Functionhx <2994114386@qq.com> --- rmw_fastrtps_cpp/src/publisher.cpp | 6 +++--- rmw_fastrtps_cpp/src/rmw_client.cpp | 12 ++++++------ rmw_fastrtps_cpp/src/rmw_service.cpp | 12 ++++++------ rmw_fastrtps_cpp/src/subscription.cpp | 12 ++++++------ rmw_fastrtps_dynamic_cpp/src/publisher.cpp | 6 +++--- rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp | 12 ++++++------ rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp | 12 ++++++------ rmw_fastrtps_dynamic_cpp/src/subscription.cpp | 6 +++--- rmw_fastrtps_shared_cpp/src/participant.cpp | 10 ++++------ 9 files changed, 43 insertions(+), 45 deletions(-) diff --git a/rmw_fastrtps_cpp/src/publisher.cpp b/rmw_fastrtps_cpp/src/publisher.cpp index 0b4aa6feb8..34cdf9f112 100644 --- a/rmw_fastrtps_cpp/src/publisher.cpp +++ b/rmw_fastrtps_cpp/src/publisher.cpp @@ -242,6 +242,9 @@ rmw_fastrtps_cpp::create_publisher( publisher->get_datawriter_qos_from_profile(topic_name, writer_qos); // Modify specific DataWriter Qos + writer_qos.endpoint().history_memory_policy = + eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + if (!participant_info->leave_middleware_default_qos) { if (participant_info->publishing_mode == publishing_mode_t::ASYNCHRONOUS) { writer_qos.publish_mode().kind = eprosima::fastdds::dds::ASYNCHRONOUS_PUBLISH_MODE; @@ -249,9 +252,6 @@ rmw_fastrtps_cpp::create_publisher( writer_qos.publish_mode().kind = eprosima::fastdds::dds::SYNCHRONOUS_PUBLISH_MODE; } - writer_qos.endpoint().history_memory_policy = - eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; - writer_qos.data_sharing().off(); } diff --git a/rmw_fastrtps_cpp/src/rmw_client.cpp b/rmw_fastrtps_cpp/src/rmw_client.cpp index c1350b71bc..7f98621094 100644 --- a/rmw_fastrtps_cpp/src/rmw_client.cpp +++ b/rmw_fastrtps_cpp/src/rmw_client.cpp @@ -312,10 +312,10 @@ rmw_create_client( subscriber->get_datareader_qos_from_profile(topic_name_fallback, reader_qos); subscriber->get_datareader_qos_from_profile(response_topic_name, reader_qos); - if (!participant_info->leave_middleware_default_qos) { - reader_qos.endpoint().history_memory_policy = - eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + reader_qos.endpoint().history_memory_policy = + eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + if (!participant_info->leave_middleware_default_qos) { reader_qos.data_sharing().off(); } @@ -374,6 +374,9 @@ rmw_create_client( publisher->get_datawriter_qos_from_profile(request_topic_name, writer_qos); // Modify specific DataWriter Qos + writer_qos.endpoint().history_memory_policy = + eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + if (!participant_info->leave_middleware_default_qos) { if (participant_info->publishing_mode == publishing_mode_t::ASYNCHRONOUS) { writer_qos.publish_mode().kind = eprosima::fastdds::dds::ASYNCHRONOUS_PUBLISH_MODE; @@ -381,9 +384,6 @@ rmw_create_client( writer_qos.publish_mode().kind = eprosima::fastdds::dds::SYNCHRONOUS_PUBLISH_MODE; } - writer_qos.endpoint().history_memory_policy = - eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; - writer_qos.data_sharing().off(); } diff --git a/rmw_fastrtps_cpp/src/rmw_service.cpp b/rmw_fastrtps_cpp/src/rmw_service.cpp index a116173db1..f5a09ee0d9 100644 --- a/rmw_fastrtps_cpp/src/rmw_service.cpp +++ b/rmw_fastrtps_cpp/src/rmw_service.cpp @@ -308,10 +308,10 @@ rmw_create_service( subscriber->get_datareader_qos_from_profile(topic_name_fallback, reader_qos); subscriber->get_datareader_qos_from_profile(request_topic_name, reader_qos); - if (!participant_info->leave_middleware_default_qos) { - reader_qos.endpoint().history_memory_policy = - eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + reader_qos.endpoint().history_memory_policy = + eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + if (!participant_info->leave_middleware_default_qos) { reader_qos.data_sharing().off(); } @@ -374,6 +374,9 @@ rmw_create_service( publisher->get_datawriter_qos_from_profile(response_topic_name, writer_qos); // Modify specific DataWriter Qos + writer_qos.endpoint().history_memory_policy = + eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + if (!participant_info->leave_middleware_default_qos) { if (participant_info->publishing_mode == publishing_mode_t::ASYNCHRONOUS) { writer_qos.publish_mode().kind = eprosima::fastdds::dds::ASYNCHRONOUS_PUBLISH_MODE; @@ -381,9 +384,6 @@ rmw_create_service( writer_qos.publish_mode().kind = eprosima::fastdds::dds::SYNCHRONOUS_PUBLISH_MODE; } - writer_qos.endpoint().history_memory_policy = - eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; - writer_qos.data_sharing().off(); } diff --git a/rmw_fastrtps_cpp/src/subscription.cpp b/rmw_fastrtps_cpp/src/subscription.cpp index 8a7b285ee0..c7e76b06f0 100644 --- a/rmw_fastrtps_cpp/src/subscription.cpp +++ b/rmw_fastrtps_cpp/src/subscription.cpp @@ -416,10 +416,10 @@ __create_dynamic_subscription( // the QoS is already the default subscriber->get_datareader_qos_from_profile(topic_name, reader_qos); - if (!participant_info->leave_middleware_default_qos) { - reader_qos.endpoint().history_memory_policy = - eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + reader_qos.endpoint().history_memory_policy = + eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + if (!participant_info->leave_middleware_default_qos) { reader_qos.data_sharing().off(); } @@ -688,10 +688,10 @@ __create_subscription( // the QoS is already the default subscriber->get_datareader_qos_from_profile(topic_name, reader_qos); - if (!participant_info->leave_middleware_default_qos) { - reader_qos.endpoint().history_memory_policy = - eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + reader_qos.endpoint().history_memory_policy = + eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + if (!participant_info->leave_middleware_default_qos) { reader_qos.data_sharing().off(); } diff --git a/rmw_fastrtps_dynamic_cpp/src/publisher.cpp b/rmw_fastrtps_dynamic_cpp/src/publisher.cpp index 5f3168e6cc..e1d361f920 100644 --- a/rmw_fastrtps_dynamic_cpp/src/publisher.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/publisher.cpp @@ -249,6 +249,9 @@ rmw_fastrtps_dynamic_cpp::create_publisher( publisher->get_datawriter_qos_from_profile(topic_name, writer_qos); // Modify specific DataWriter Qos + writer_qos.endpoint().history_memory_policy = + eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + if (!participant_info->leave_middleware_default_qos) { if (participant_info->publishing_mode == publishing_mode_t::ASYNCHRONOUS) { writer_qos.publish_mode().kind = eprosima::fastdds::dds::ASYNCHRONOUS_PUBLISH_MODE; @@ -256,9 +259,6 @@ rmw_fastrtps_dynamic_cpp::create_publisher( writer_qos.publish_mode().kind = eprosima::fastdds::dds::SYNCHRONOUS_PUBLISH_MODE; } - writer_qos.endpoint().history_memory_policy = - eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; - writer_qos.data_sharing().off(); } diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp index ff04e63cec..9068fd1ea5 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp @@ -341,10 +341,10 @@ rmw_create_client( subscriber->get_datareader_qos_from_profile(topic_name_fallback, reader_qos); subscriber->get_datareader_qos_from_profile(response_topic_name, reader_qos); - if (!participant_info->leave_middleware_default_qos) { - reader_qos.endpoint().history_memory_policy = - eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + reader_qos.endpoint().history_memory_policy = + eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + if (!participant_info->leave_middleware_default_qos) { reader_qos.data_sharing().off(); } @@ -403,6 +403,9 @@ rmw_create_client( publisher->get_datawriter_qos_from_profile(request_topic_name, writer_qos); // Modify specific DataWriter Qos + writer_qos.endpoint().history_memory_policy = + eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + if (!participant_info->leave_middleware_default_qos) { if (participant_info->publishing_mode == publishing_mode_t::ASYNCHRONOUS) { writer_qos.publish_mode().kind = eprosima::fastdds::dds::ASYNCHRONOUS_PUBLISH_MODE; @@ -410,9 +413,6 @@ rmw_create_client( writer_qos.publish_mode().kind = eprosima::fastdds::dds::SYNCHRONOUS_PUBLISH_MODE; } - writer_qos.endpoint().history_memory_policy = - eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; - writer_qos.data_sharing().off(); } diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp index b4a6143d23..3704d4c1a8 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp @@ -337,10 +337,10 @@ rmw_create_service( subscriber->get_datareader_qos_from_profile(topic_name_fallback, reader_qos); subscriber->get_datareader_qos_from_profile(request_topic_name, reader_qos); - if (!participant_info->leave_middleware_default_qos) { - reader_qos.endpoint().history_memory_policy = - eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + reader_qos.endpoint().history_memory_policy = + eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + if (!participant_info->leave_middleware_default_qos) { reader_qos.data_sharing().off(); } @@ -403,6 +403,9 @@ rmw_create_service( publisher->get_datawriter_qos_from_profile(response_topic_name, writer_qos); // Modify specific DataWriter Qos + writer_qos.endpoint().history_memory_policy = + eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + if (!participant_info->leave_middleware_default_qos) { if (participant_info->publishing_mode == publishing_mode_t::ASYNCHRONOUS) { writer_qos.publish_mode().kind = eprosima::fastdds::dds::ASYNCHRONOUS_PUBLISH_MODE; @@ -410,9 +413,6 @@ rmw_create_service( writer_qos.publish_mode().kind = eprosima::fastdds::dds::SYNCHRONOUS_PUBLISH_MODE; } - writer_qos.endpoint().history_memory_policy = - eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; - writer_qos.data_sharing().off(); } diff --git a/rmw_fastrtps_dynamic_cpp/src/subscription.cpp b/rmw_fastrtps_dynamic_cpp/src/subscription.cpp index 4c7f2acd9f..4c2a2eb4a0 100644 --- a/rmw_fastrtps_dynamic_cpp/src/subscription.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/subscription.cpp @@ -257,10 +257,10 @@ create_subscription( // the QoS is already the default subscriber->get_datareader_qos_from_profile(topic_name, reader_qos); - if (!participant_info->leave_middleware_default_qos) { - reader_qos.endpoint().history_memory_policy = - eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + reader_qos.endpoint().history_memory_policy = + eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + if (!participant_info->leave_middleware_default_qos) { reader_qos.data_sharing().off(); } diff --git a/rmw_fastrtps_shared_cpp/src/participant.cpp b/rmw_fastrtps_shared_cpp/src/participant.cpp index bb5d6c734a..d7cc1e16a4 100644 --- a/rmw_fastrtps_shared_cpp/src/participant.cpp +++ b/rmw_fastrtps_shared_cpp/src/participant.cpp @@ -329,12 +329,10 @@ rmw_fastrtps_shared_cpp::create_participant( } } // allow reallocation to support discovery messages bigger than 5000 bytes - if (!leave_middleware_default_qos) { - domainParticipantQos.wire_protocol().builtin.readerHistoryMemoryPolicy = - eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; - domainParticipantQos.wire_protocol().builtin.writerHistoryMemoryPolicy = - eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; - } + domainParticipantQos.wire_protocol().builtin.readerHistoryMemoryPolicy = + eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; + domainParticipantQos.wire_protocol().builtin.writerHistoryMemoryPolicy = + eprosima::fastdds::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE; if (security_options->security_root_path) { // if security_root_path provided, try to find the key and certificate files #if HAVE_SECURITY