Skip to content

fix: release RingBuffer elements as PushBack evicts them [patch] - #99

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/ringbuffer-evicted-slot-85
Oct 6, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/ringbuffer-evicted-slot-85

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Summary

When the buffer was full, RingBuffer<T>.PushBack advanced FrontIndex past the evicted element without clearing that element's slot. The backing array is NextPower2(Length) long. If Length is 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.

PushBack now clears the evicted slot before advancing FrontIndex, then writes the new element. Writing last keeps Length == Capacity correct, because there the evicted slot is the one being written.

Tests

  • PushBack_ReleasesReferenceToEvictedElement: in a RingBuffer<object>(5) (capacity 8), the evicted element is collected after a full GC. I ran it against the unfixed code first and it failed on Assert.IsFalse(references[0].IsAlive). It passes with the fix.
  • PushBack_FullPowerOfTwoBuffer_KeepsNewestElements protects the Length == Capacity ordering.
  • Full suite: 425/425 pass locally (net10.0).

Fixes #85

🤖 Generated with Claude Code

https://claude.ai/code/session_01UoSUYMQMykgfsb5y6AhnKb


Generated by Claude Code

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
@sonarqubecloud

sonarqubecloud Bot commented Oct 6, 2026

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 11c1b49 into main Oct 6, 2026
14 checks passed
@matt-edmondson
matt-edmondson deleted the fix/ringbuffer-evicted-slot-85 branch October 6, 2026 02:11
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.

RingBuffer keeps up to Length−1 evicted elements alive when Length is not a power of two

2 participants