Skip to content

mavros_extras: Fix dangling pointers and uninitialized memory with Eigen#2221

Open
klaxalk wants to merge 1 commit into
mavlink:ros2from
klaxalk:fix_eigen
Open

mavros_extras: Fix dangling pointers and uninitialized memory with Eigen#2221
klaxalk wants to merge 1 commit into
mavlink:ros2from
klaxalk:fix_eigen

Conversation

@klaxalk

@klaxalk klaxalk commented Jun 21, 2026

Copy link
Copy Markdown

While testing builds with GCC 15.2 (while adopting Nix which has new versions of underlying libraries and compilers), I noticed several -Wdangling-pointer and -Wmaybe-uninitialized warnings that were causing undefined behavior and silent memory corruption.

The issue stems from using auto to store the result of Eigen math formulas (e.g., auto vec = Vector3d(...) * M_PI). Under the hood, this evaluates to an expression template rather than an actual vector. Since the original temporary vector gets destroyed immediately, the expression template ends up holding a dangling pointer.

To fix this, I swapped the auto declarations for explicit Eigen::Vector3d types in hil.cpp and mount_control.cpp to force immediate evaluation. I also zero-initialized a vector in landing_target.cpp to clear out an uninitialized memory warning.

Tested locally and everything compiles cleanly now:
Summary: 415 tests, 0 errors, 0 failures, 100 skipped

…n expressions

Swapped `auto` for explicit `Eigen::VectorX` types in hil.cpp
and mount_control.cpp to prevent expression templates from holding
dangling pointers to temporary objects.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant