Skip to content

Use lower_bound() before iterating multimap entries of a key (fixes getConnectedGraph abort on macOS) - #1776

Merged
matlabbe merged 2 commits into
introlab:masterfrom
ntotten:fix-multimap-find-lower-bound
Sep 28, 2026
Merged

matlabbe merged 2 commits into
introlab:masterfrom
ntotten:fix-multimap-find-lower-bound

Conversation

@ntotten

@ntotten ntotten commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

Requested by Nate

Before: on macOS with the current Apple toolchain, including the Homebrew rtabmap 0.23.8 bottle, any optimization of a graph with loop closures or several sessions aborts:

[FATAL] Optimizer.cpp:295::getConnectedGraph() Condition (kter!=linksIn.end()) not met!

It reproduces with rtabmap-export --poses --opt 0 map.db on a multi-session iOS-app database, and with rtabmap-reprocess "a.db;b.db" out.db as 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 iterate while(iter != links.end() && iter->first == id), assuming find() returns the first element with that key. std::multimap::find only 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 return lower_bound(key). As a result, graph::findLink() misses links that exist, and getConnectedGraph() asserts.

How: every find-then-iterate site now uses lower_bound() instead of find(). There are 23 one-line changes in Graph.cpp (all findLink overloads, and the loop closure filtering), Optimizer.cpp (getConnectedGraph), Rtabmap.cpp, Signature.cpp and DatabaseViewer.cpp. Behaviour on libstdc++ is unchanged. The PR also adds GraphTest.FindLinkWithManyLinksPerNode.

Testing on macOS 27 / Apple clang 21, with test_corelib built from master:

  • Without the fix, 4 tests fail: the new test plus the existing OptimizerTest.GetConnectedGraphFollowsLoopClosureLinks, GetConnectedGraphPriorLinksIncludedWhenAllowed and GetConnectedGraphPriorLinksDroppedWhenIgnored.
  • With the fix, all 1621 tests pass except 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.
  • Applied to 0.23.8, the fix also lets rtabmap-export --opt 0, rtabmap-detectMoreLoopClosures and a five-database rtabmap-reprocess finish on real iPhone LiDAR databases that aborted before.

🤖 Generated with Claude Code

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]>
@ntotten

ntotten commented Sep 27, 2026

Copy link
Copy Markdown
Contributor Author

Obviously AI, so feel free to close, but this solved the issue i was having when running on my Mac.

@matlabbe matlabbe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@matlabbe
matlabbe merged commit 5c9cfa9 into introlab:master Sep 28, 2026
33 checks passed
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.

2 participants