Repository navigation
fix: release RingBuffer elements as PushBack evicts them [patch] - #99
Merged
Merged
Conversation
PushBack advanced FrontIndex past the evicted element without clearing its slot. When Length is not a power of two the backing array is larger than the live window, so up to Length-1 evicted elements stayed reachable until their slot happened to be overwritten again. Clear the evicted slot before advancing, then write the new element, so the Length == Capacity case (where the evicted slot is the one being written) still keeps the new element. Fixes #85 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UoSUYMQMykgfsb5y6AhnKb
|
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.



Summary
When the buffer was full,
RingBuffer<T>.PushBackadvancedFrontIndexpast the evicted element without clearing that element's slot. The backing array isNextPower2(Length)long. IfLengthis not a power of two, the slots outside the live window keep evicted elements reachable until something overwrites them, and nothing does once pushing stops.PushBacknow clears the evicted slot before advancingFrontIndex, then writes the new element. Writing last keepsLength == Capacitycorrect, because there the evicted slot is the one being written.Tests
PushBack_ReleasesReferenceToEvictedElement: in aRingBuffer<object>(5)(capacity 8), the evicted element is collected after a full GC. I ran it against the unfixed code first and it failed onAssert.IsFalse(references[0].IsAlive). It passes with the fix.PushBack_FullPowerOfTwoBuffer_KeepsNewestElementsprotects theLength == Capacityordering.Fixes #85
🤖 Generated with Claude Code
https://claude.ai/code/session_01UoSUYMQMykgfsb5y6AhnKb
Generated by Claude Code