Fix std::bad_alloc - #80
Merged
Merged
Conversation
Signed-off-by: Francisco Martín Rico <fmrico@gmail.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a use-after-scope bug in multiple map manager plugins where async subscription callbacks captured a const auto& tf_info bound to a temporary returned by RTTFBuffer::get_tf_info() (by value), leading to dangling std::string reads and std::bad_alloc at runtime.
Changes:
- Replaced async-callback uses of
tf_info.map_framewith fresh per-callback snapshot reads viaRTTFBuffer::getInstance()->get_tf_info().map_frame. - Removed the now-unused outer
tf_inforeference inOctomapMapsManagerand updated TF lookup / message header assignment accordingly. - Kept synchronous (in-scope) uses of
tf_infoinsideon_initialize()unchanged.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| maps_managers/easynav_simple_maps_manager/src/easynav_simple_maps_manager/SimpleMapsManager.cpp | Avoids capturing a dangling tf_info in the incoming map subscription callback by fetching map_frame at point of use. |
| maps_managers/easynav_octomap_maps_manager/src/easynav_octomap_maps_manager/OctomapMapsManager.cpp | Removes outer tf_info reference and fetches map_frame inside the PointCloud2 callback for TF lookup and message headers. |
| maps_managers/easynav_navmap_maps_manager/src/easynav_navmap_maps_manager/NavMapMapsManager.cpp | Fixes two async callbacks to use a fresh map_frame snapshot instead of a captured temporary reference. |
| maps_managers/easynav_costmap_maps_manager/src/easynav_costmap_maps_manager/CostmapMapsManager.cpp | Fixes the incoming map subscription callback to read map_frame from a fresh get_tf_info() snapshot. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi,
This PR fixes a dangling
tf_inforeference in async map-topic callbacks.RTTFBuffer::get_tf_info()returnsTFInfoby value (a locked snapshot copy). Severalon_initialize()methods did:Temporary lifetime extension only keeps the temporary alive until
tf_infoitself goes out of scope, i.e. untilon_initialize()returns. The subscription lambda capturestf_infoby reference ([&]) and is invoked later, after that scope has ended, sotf_infois dangling by the time a message arrives. Readingtf_info.map_framethen interprets freed memory as astd::string, producing a garbage size and anoperator newcall that throwsstd::bad_alloc.To fix it, inside each async callback, fetch a fresh snapshot at the point of use (
RTTFBuffer::getInstance()->get_tf_info().map_frame) instead of reusing the captured reference. Synchronous uses oftf_infowithinon_initialize()'s own scope are untouched as those are safe.This bug affected to :
easynav_costmap_maps_manager—CostmapMapsManager.cppeasynav_simple_maps_manager—SimpleMapsManager.cppeasynav_navmap_maps_manager—NavMapMapsManager.cpp(two callbacks:incoming_occ_map,incoming_pc2_map)easynav_octomap_maps_manager—OctomapMapsManager.cpp(incoming_pc2_map; also dropped the now-unused outertf_infolocal)