Repository navigation
Use lower_bound() before iterating multimap entries of a key (fixes getConnectedGraph abort on macOS) - #1776
Merged
Conversation
Several places look up a multimap with find(key) and then iterate while iter->first == key, assuming find() returns the first element with that key. The standard does not guarantee this, and recent libc++ (Apple clang 21 / libc++ 2200) returns an arbitrary matching element. graph::findLink() then misses existing links and Optimizer::getConnectedGraph() aborts with "Condition (kter!=linksIn.end()) not met!" on graphs with loop closures or multiple sessions (rtabmap-export --opt 0, rtabmap-reprocess, etc.). Replace find() with lower_bound() at those sites and add a regression test. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Contributor
Author
|
Obviously AI, so feel free to close, but this solved the issue i was having when running on my Mac. |
matlabbe
approved these changes
Sep 27, 2026
matlabbe
left a comment
Member
There was a problem hiding this comment.
I think it looks fine. Indeed, it looks like we always [wrongly] assumed that find() on a multimap returned the first key of a sequence with same key. Seems that all older compilers were implementing find() on top of a lower_bound() and an equal check.
From cppreference:
Finds an element with key equivalent to key. If there are several elements with the requested key in the container, any of them may be returned.
2 tasks done
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.
Requested by Nate
Before: on macOS with the current Apple toolchain, including the Homebrew
rtabmap0.23.8 bottle, any optimization of a graph with loop closures or several sessions aborts:It reproduces with
rtabmap-export --poses --opt 0 map.dbon a multi-session iOS-app database, and withrtabmap-reprocess "a.db;b.db" out.dbas soon as the first inter-session loop closure is added.After: these graphs optimize normally.
The cause is that several places do
iter = links.find(id)and then iteratewhile(iter != links.end() && iter->first == id), assumingfind()returns the first element with that key.std::multimap::findonly guarantees some matching element. Recent libc++ (Apple clang 21, libc++ 2200) returns a non-first one: a quick test found 464 of 500 lookups on a multimap with repeated keys did not returnlower_bound(key). As a result,graph::findLink()misses links that exist, andgetConnectedGraph()asserts.How: every find-then-iterate site now uses
lower_bound()instead offind(). There are 23 one-line changes inGraph.cpp(allfindLinkoverloads, and the loop closure filtering),Optimizer.cpp(getConnectedGraph),Rtabmap.cpp,Signature.cppandDatabaseViewer.cpp. Behaviour on libstdc++ is unchanged. The PR also addsGraphTest.FindLinkWithManyLinksPerNode.Testing on macOS 27 / Apple clang 21, with
test_corelibbuilt from master:OptimizerTest.GetConnectedGraphFollowsLoopClosureLinks,GetConnectedGraphPriorLinksIncludedWhenAllowedandGetConnectedGraphPriorLinksDroppedWhenIgnored.GraphTest.ComputePathDijkstraWeighted, which fails the same way before and after this change (path size 2 vs 3), so it looks like a separate macOS issue.rtabmap-export --opt 0,rtabmap-detectMoreLoopClosuresand a five-databasertabmap-reprocessfinish on real iPhone LiDAR databases that aborted before.🤖 Generated with Claude Code