← Back to Pull requests

Avoid unnecessary std::allocator<void> in MessageMemoryStrategy::create_default() #3436

Open
unknown opened this pull request with 618 lines diff across 7 files
View on GitHub
unknown unknown commented on Nov 25, 2026 13:47 UTC
Author

Description

MessageMemoryStrategy::create_default() previously constructed a std::make_shared<std::allocator<void>>() and passed it to the allocator constructor even for the default Alloc = std::allocator<void>. Because std::allocator<void> is an empty stateless type, this performs a redundant heap allocation on every default subscription and dereferencing the shared pointer inside the constructor triggers a GCC 15 -Werror=uninitialized false positive in downstream packages compiled with optimization.

Changes:

  • Update MessageMemoryStrategy::create_default() in rclcpp/include/rclcpp/message_memory_strategy.hpp to call the default MessageMemoryStrategy() constructor when Alloc is std::allocator<void>, while preserving existing behavior for custom allocators.

  • Add #include <type_traits> to rclcpp/include/rclcpp/message_memory_strategy.hpp for std::is_same_v.

  • Add TEST(TestMemoryStrategies, custom_allocator_create_default) in rclcpp/test/rclcpp/test_message_memory_strategy.cpp to cover the custom-allocator branch of create_default().
  • GCC 15 Diagnostic

    Observed with x86_64-conda-linux-gnu-c++ (conda-forge gcc 15.3.0-7) 15.3.0 and -std=gnu++23 -O2 -g -DNDEBUG -fPIC -Wall -Wextra -Werror against rclcpp (Jazzy 28.1.22, where create_default() is at message_memory_strategy.hpp:91; identical code on rolling at line 98) in a constructor with multiple create_subscription() calls:

    In file included from <prefix>/lib/gcc/x86_64-conda-linux-gnu/15.3.0/include/c++/memory:82,
                     from <prefix>/include/rclcpp/rclcpp/clock.hpp:19,
                     from <downstream_pkg>/include/<downstream_pkg>/my_node.hpp:21,
                     from <downstream_pkg>/src/my_node.cpp:15:
    In function 'std::shared_ptr<std::_NonArray<_Tp> > std::make_shared(_Args&& ...) [with _Tp = allocator<void>; _Args = {}]',
        inlined from 'static rclcpp::message_memory_strategy::MessageMemoryStrategy<MessageT, Alloc>::SharedPtr rclcpp::message_memory_strategy::MessageMemoryStrategy<MessageT, Alloc>::create_default() [with MessageT = nav_msgs::msg::Odometry_<std::allocator<void> >; Alloc = std::allocator<void>]' at <prefix>/include/rclcpp/rclcpp/message_memory_strategy.hpp:91:92,
        inlined from 'MyNode::MyNode(const rclcpp::Node::SharedPtr&, ...)' at <downstream_pkg>/src/my_node.cpp:74:57:
    <prefix>/lib/gcc/x86_64-conda-linux-gnu/15.3.0/include/c++/bits/shared_ptr.h:1008:60: error: 'SR.93144' is used uninitialized [-Werror=uninitialized]
     1008 |                              std::forward<_Args>(__args)...);
          |                                                            ^
    In file included from <prefix>/include/rclcpp/rclcpp/subscription.hpp:42,
                     from <prefix>/include/rclcpp/rclcpp/any_executable.hpp:25,
                     from <prefix>/include/rclcpp/rclcpp/memory_strategy.hpp:25,
                     from <prefix>/include/rclcpp/rclcpp/memory_strategies.hpp:18,
                     from <prefix>/include/rclcpp/rclcpp/executor_options.hpp:22,
                     from <prefix>/include/rclcpp/rclcpp/executor.hpp:38,
                     from <prefix>/include/rclcpp/rclcpp/executors/multi_threaded_executor.hpp:25,
                     from <prefix>/include/rclcpp/rclcpp/executors.hpp:21,
                     from <prefix>/include/rclcpp/rclcpp/rclcpp.hpp:176,
                     from <prefix>/include/tf2_ros/tf2_ros/buffer_interface.hpp:50,
                     from <prefix>/include/tf2_ros/tf2_ros/buffer.hpp:45,
                     from <prefix>/include/tf2_ros/tf2_ros/buffer.h:35,
                     from <downstream_pkg>/include/<downstream_pkg>/my_node.hpp:24:
    <prefix>/include/rclcpp/rclcpp/message_memory_strategy.hpp: In constructor 'MyNode::MyNode(const rclcpp::Node::SharedPtr&, ...)':
    <prefix>/include/rclcpp/rclcpp/message_memory_strategy.hpp:91:92: note: 'SR.93144' was declared here
       91 |     return std::make_shared<MessageMemoryStrategy<MessageT, Alloc>>(std::make_shared<Alloc>());
          |                                                                     ~~~~~~~~~~~~~~~~~~~~~~~^~
    cc1plus: all warnings being treated as errors
    

    Applying this patch to message_memory_strategy.hpp eliminated the error on the failing translation unit without needing -Wno-uninitialized.

    Is this user-facing behavior change?


    No

    Did you use Generative AI?

    Yes, Antigravity (Gemini) assisted with writing the unit test and PR description.

    Additional Information

  • Backporting: This change is header-only and ABI-compatible, and applies cleanly to lyrical, jazzy, and humble if maintainers would like to backport it.

  • Local verification:

  • - ament_uncrustify and ament_cpplint passed with no issues on both modified files.
    - Compiled and ran test_message_memory_strategy.cpp with GCC 15.3.0 (-std=c++17 -O2 -Wall -Wextra -Werror); all 3 tests passed with no warnings.

    Diff: 618 lines (+463 / -155) across 7 files Reviewer: jmachowinski Review duration: 7299m
    All Vouch review confidence checks passed
    Residual risk 0.27 = Change Risk (0.50) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 618 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.385
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #3436"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.2% (MEDIUM) · Higher = larger blast radius
    50.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 26.8% · Safe to merge
    26.8%
    Repository