Repository navigation
fix(deque): free orphaned chunks in grow_map to stop chunked-deque leak (closes #3286) - #3289
Conversation
…ak (closes #3286) Regression introduced by #3282 (chunked map-of-fixed-size-blocks layout, commit a8b5cac). Surfaced as macOS-arm64 unit test failure fl_audio_detectors with ASAN LeakSanitizer reporting ~5KB leaked in 20 allocations, all rooted in fl::deque<float>::ensure_back_room() → DefaultMemoryResource::do_allocate(). Linux x86-64 jobs don't run leak-detection, so the regression only tripped CI on the macos workflow. Root cause ---------- `pop_front()` / `pop_back()` advance mFrontMapIdx / mSize past an emptied chunk without freeing or nulling the chunk pointer. The chunk is left orphaned in mMap[]. The destructor sweeps the entire map so orphans are harmless as long as the map itself survives. But `grow_map()` only copies pointers in [mFrontMapIdx, mFrontMapIdx+used) forward into the new map; it then deallocates the old map. Every orphaned chunk pointer outside that range is silently lost, leaking its backing storage. The audio TempoAnalyzer hits this directly: mOnsetTimes (50-entry sliding window) and mBPMHistory (20-entry sliding window) do push_back + pop_front each frame. After enough frames, an old chunk gets orphaned, then a later push_back forces grow_map, then the orphan leaks. Fix --- In `grow_map()`, after a successful new-map allocation but before deallocating the old map, free any chunk pointers in the old map that fall outside the live [mFrontMapIdx, mFrontMapIdx+used) range. The live range still copies forward unchanged, so chunk addresses remain stable for elements that survive a grow (preserving the #3270 design intent of pointer / iterator stability). Test ---- tests/fl/stl/deque.cpp gains `fl::deque - grow_map releases orphaned chunks (#3286)`. It uses SmallChunkTag (chunk_size=4) to reproduce the push/pop/push-grow leak pattern in a few dozen ops instead of the thousands needed at the default chunk_size=64; ASAN under macOS-arm64 flags the leak immediately on the broken code. Verification - bash test fl_stl_deque --debug → pass (Windows clang+ASAN) - bash test fl_audio_detectors --debug → pass (Windows clang+ASAN) - bash lint --cpp → clean References - Breaking PR: #3282 (commit a8b5cac) - Regression issue: #3286 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughIn Changesgrow_map orphaned chunk cleanup
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related issues
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
Fixes the macOS-arm64 unit-test regression that's been red on master since #3282 (commit a8b5cac). LeakSanitizer reported ~5KB leaked in 20 allocations rooted in
fl::deque<float>::ensure_back_room()fromTempoAnalyzer::updateCurrentTempo/update.Closes #3286.
Root cause
The chunked-deque refactor in #3282 has a chunk-orphaning bug in the pop / grow interaction:
pop_front()/pop_back()advancemFrontMapIdx/mSizepast an emptied chunk without freeing or nulling the chunk pointer. The chunk is left orphaned inmMap[].grow_map()only copies pointers in[mFrontMapIdx, mFrontMapIdx+used)forward into the new map, then deallocates the old map. Every orphaned chunk pointer outside that range is silently lost, leaking its backing storage.The audio
TempoAnalyzerhits this directly:mOnsetTimes(50-entry sliding window) andmBPMHistory(20-entry sliding window) do push_back + pop_front each frame. After enough frames, an old chunk gets orphaned, then a later push_back forces grow_map, then the orphan leaks. Linux x86-64 CI doesn't run leak detection so the regression only tripped the macos job.Fix
In
grow_map(), after a successful new-map allocation but before deallocating the old map, free any chunk pointers in the old map that fall outside the live[mFrontMapIdx, mFrontMapIdx+used)range. The live range still copies forward unchanged, so chunk addresses stay stable for surviving elements (preserves the #3270 pointer / iterator stability guarantee).Two-file diff:
src/fl/stl/deque.h— the orphan-cleanup loop insidegrow_maptests/fl/stl/deque.cpp— regression test usingSmallChunkTag(chunk_size=4) to reproduce the leak pattern in a few dozen opsTest plan
bash test fl_stl_deque --debug(clang + ASAN) → passbash test fl_audio_detectors --debug(clang + ASAN) → passbash lint --cpp→ cleanReferences
Summary by CodeRabbit
Bug Fixes
Tests