← Back to Pull requests

[MPPI] Destroy the motion model before its loader #6778

Merged
MatthijsBurgh merged 1 files (4 lines) on Nov 17, 2026 19:05 UTC
View on GitHub
MatthijsBurgh MatthijsBurgh commented on Nov 12, 2026 08:08 UTC
Author

<!-- Please fill out the following pull request template for non-trivial changes to help us process your PR faster and more efficiently.-->

---

Basic Info

| Info | Please fill out this column |
| ------ | ----------- |
| Ticket(s) this addresses | none (found by reading the code, then confirmed with the existing unit tests) |
| Primary OS tested on | Ubuntu (ros:rolling-ros-base Docker image) |
| Robotic platform tested on | none, unit tests only |
| Does this PR contain AI generated software? | Yes. It was written with Claude Code and checked by building nav2_mppi_controller from source and running its unit tests before and after the change, plus ament_uncrustify and ament_cpplint |
| Was this PR description generated by AI software? | Yes, written with Claude Code for me; I have checked the PR and the description myself |

---

Description of contribution in a few bullet points

Bug: since the motion models became plugins (#6076), every mppi::Optimizer destruction prints

  Warning: class_loader.ClassLoader: SEVERE WARNING!!! Attempting to unload library while objects created by this loader exist in the heap! ... The library will NOT be unloaded.
  

Cause: Optimizer declares motion_model_ (optimizer.hpp) before motion_model_loader_, and ~Optimizer() only calls shutdown(). Members are destroyed in reverse order, so the pluginlib::ClassLoader goes while the shared motion model instance still exists (held by motion_model_ and by critics_data_.motion_model). setMotionModel() has the same problem when it replaces an existing loader, for example on configure after cleanup.
Fix: release both references to the motion model in Optimizer::shutdown(), which the destructor already calls, and before setMotionModel() creates a new loader. The header is unchanged, so the patch can be backported.
Affected branches:

| Branch | Affected | Patch applies | How checked |
|---|---|---|---|
| main | yes | this PR | reproduced, fix verified |
| lyrical | yes (has the pluginized motion models) | yes, clean cherry-pick | by inspection |
| kilted, jazzy, humble | no | n/a | by inspection: the motion model is not a plugin there |

Could a maintainer add the backport-lyrical label?

Description of documentation updates required from your changes

None.

Description of how this change was tested

Built nav2_mppi_controller (Release, tests on) from main in ros:rolling-ros-base, ran these test binaries and counted the SEVERE WARNING lines:

| Test binary | main | main + this PR |
|---|---|---|
| controller_state_transition_test (1 test) | 2 | 1 |
| optimizer_unit_tests (22 tests) | 43 | 22 |
| optimizer_smoke_test (3 tests) | 6 | 3 |

All tests pass before and after. With gdb I checked the warning that remains in controller_state_transition_test: it comes from nav2_costmap_2d::Costmap2DROS::~Costmap2DROS() (the test's costmap is destroyed without on_cleanup()), not from MPPI. The remaining warnings in the other two binaries come one per test, which matches the same costmap source, but I did not trace each of them.
ament_uncrustify and ament_cpplint report no problems for the changed files.

---

Future work that may be required in bullet points

None for this change.

For Maintainers: <!-- DO NOT EDIT OR REMOVE -->


  • Check that any new parameters added are updated in docs.nav2.org

  • Check that any significant change is added to the migration guide

  • Check that any new features OR changes to existing behaviors are reflected in the tuning guide

  • Check that any new functions have Doxygen added

  • Check that any new features have test coverage

  • Check that any new plugins is added to the plugins page

  • If BT Node, Additionally: add to BT's XML index of nodes for groot, BT package's readme table, and BT library lists

  • Should this be backported to current distributions? If so, tag with backport-*.
  • 🤖 Generated with Claude Code

    Diff: 4 lines (+4 / -0) across 1 files Reviewer: SteveMacenski Review duration: 7856m
    All Vouch review confidence checks passed
    Residual risk 0.03 = Change Risk (0.18) × (1 − Review Confidence (0.84)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.18 · 4 lines diff across 1 files
    0.18
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.78 · 4 substantive comment(s)
    0.78
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    84%
    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.096
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    7 items
    codecov[bot] codecov[bot] commented on Nov 12, 2026 10:46 UTC
    Bot

    Codecov Report


    :white_check_mark: All modified and coverable lines are covered by tests.

    | Files with missing lines | Coverage Δ | |
    |---|---|---|
    | nav2\_mppi\_controller/src/optimizer.cpp | 96.22% <ø> (ø) | |

    ... and 5 files with indirect coverage changes
    <details><summary> :rocket: New features to boost your workflow: </summary>

  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

  • </details>

    SteveMacenski SteveMacenski commented on code on Nov 12, 2026 18:47 UTC
    Code Review
    nav2_mppi_controller/src/optimizer.cpp:1
    @@ -69,6 +69,14 @@ void Optimizer::initialize(
       reset();
     }
     
    +Optimizer::~Optimizer()
    +{
    +  shutdown();
    +  // The motion model must be destroyed before motion_model_loader_, which is declared after it
    +  critics_data_.motion_model.reset();
    +  motion_model_.reset();

    Should this happen in shutdown instead?

    Also remove the comment

    SteveMacenski SteveMacenski commented on Nov 12, 2026 18:47 UTC
    Reviewer

    Generally LGTM

    MatthijsBurgh MatthijsBurgh commented on code on Nov 17, 2026 12:17 UTC
    Code Review
    nav2_mppi_controller/src/optimizer.cpp:1
    @@ -69,6 +69,14 @@ void Optimizer::initialize(
       reset();
     }
     
    +Optimizer::~Optimizer()
    +{
    +  shutdown();
    +  // The motion model must be destroyed before motion_model_loader_, which is declared after it
    +  critics_data_.motion_model.reset();
    +  motion_model_.reset();

    I moved this to shutdown.

    MatthijsBurgh MatthijsBurgh commented on Nov 17, 2026 12:18 UTC
    Author

    @SteveMacenski I have addressed your comments.

    SteveMacenski SteveMacenski approved these changes on Nov 17, 2026 19:04 UTC
    Approved Reviewer

    Approved these changes.

    Reviewers
    Assignees
    MatthijsBurgh MatthijsBurgh
    Labels
    risk: low confidence: 84.5% 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: 17.6% (LOW) · Higher = larger blast radius
    17.6%
    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: 84.5% · Lower = rushed or shallow review
    84.5%
    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: 2.7% · Safe to merge
    2.7%