Skip to content

densify: protect the reference depth-map before loading it in fusion - #1321

Closed
Tobias-Fischer wants to merge 1 commit into
cdcseacave:developfrom
Tobias-Fischer:upstream-fusion-cache
Closed

Tobias-Fischer wants to merge 1 commit into
cdcseacave:developfrom
Tobias-Fischer:upstream-fusion-cache

Conversation

@Tobias-Fischer

Copy link
Copy Markdown

DMapCache::UseImage() loads a depth-map and then ejects the least recently used ones until the cache fits its budget. Both fusion loops (FuseDepthMaps, DenseFuseDepthMaps) mark the reference depth-map as protected with SkipMemoryCheckIdxImage() only after loading it, so the ejection inside UseImage() can release the reference itself whenever the budget is smaller than that depth-map.

This PR protects the reference before loading it, and clears the protection again when the depth-map turns out to be unreadable. Neighbors loaded afterwards are newer than the reference in the LRU order, so the ejection stops at the reference and keeps them too.

This is a hardening change: I have no reproducer on develop. Since #1309 the fusion budget is floored at one working set (FusionCacheSetup::Available()), so the budget should never drop below the reference depth-map there. If it ever did, the IsEmpty() check would skip the reference, and its points would be silently missing from the cloud.

Where it does happen: 2.4.0. There GetAvailableMemory() can return a budget of a few MB when free memory is just above the safety margin, and the loop has no IsEmpty() check. DenseFuseDepthMaps() then dereferences the released reference depth-map:

EXC_BAD_ACCESS (address=0x0)
  DenseFuseDepthMaps(...)::$_0::operator()  SceneDensify.cpp:1685  depthData.depthMap(x)   [ID=0, x=(0,0), fuseDepth=0]
  DenseFuseDepthMaps(...)                   SceneDensify.cpp:1833
  Scene::DenseReconstruction(...)

It showed up at random on a 24 GB Apple M5 under memory pressure (1 crash in 8 runs). With the cache budget capped to 1 MB through a local debug override it crashed every time; with the same reordering applied to 2.4.0 it no longer crashes, and unconstrained runs are unchanged.

Testing: the equivalent change was built and tested on 2.4.0 (macOS arm64) only. I could not build develop locally, because the new halfmesh and TinyEXIF dependencies are not packaged in my environment, so I'm relying on CI for this branch.

🤖 Generated with Claude Code

DMapCache::UseImage() loads a depth-map and then ejects the least recently
used ones until the cache fits its budget. Both fusion loops marked the
reference depth-map as protected (SkipMemoryCheckIdxImage) only after
loading it, so the ejection inside UseImage() could release the reference
itself if the budget were smaller than that depth-map.

Since the fusion budget is floored at one working set (cdcseacave#1309) this should
not happen on develop; it is a hardening change. On 2.4.0, where the budget
could drop below one depth-map under memory pressure, the same order made
DenseFuseDepthMaps() dereference the released reference depth-map.

Protect the reference before loading it (and clear the protection when the
depth-map turns out to be unreadable). Neighbors loaded afterwards are newer
than the reference in the LRU order, so they are kept as well.
@cdcseacave

Copy link
Copy Markdown
Owner

Thank you for the excellent diagnosis and the 2.4.0 crash trace. #1324 fixes it inside the cache instead of at the callers: UseImage()/Eject() never eject the depth-map just requested, and UseImage() asserts it is loaded on return, so every caller is covered without having to protect the image beforehand. The PR adds a regression test, and you are credited as co-author. Closing in favor of #1324.

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