From 6808493956a3216f4588350077c7440e505e1900 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Fri, 24 Apr 2020 12:32:52 -0600 Subject: [PATCH 1/2] Add test to verify that `Sequence.Reset()` doesn't throw Repro for #182 --- src/Nerdbank.Streams.Tests/SequenceTests.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Nerdbank.Streams.Tests/SequenceTests.cs b/src/Nerdbank.Streams.Tests/SequenceTests.cs index 3e3914ef..88c92fa7 100644 --- a/src/Nerdbank.Streams.Tests/SequenceTests.cs +++ b/src/Nerdbank.Streams.Tests/SequenceTests.cs @@ -45,6 +45,15 @@ public void GetMemory_Sizes() Assert.Throws(() => seq.GetMemory(-1)); } + [Fact] + public void Reset_AfterPartialAdvance() + { + var seq = new Sequence(new MockMemoryPool { Contents = { new object[4] } }); + seq.Write(new object[3]); + seq.AdvanceTo(seq.AsReadOnlySequence.GetPosition(2)); + seq.Reset(); + } + [Fact] public void MemoryPool_ReleasesReferenceOnRecycle() { From f585292fdfe345b00b40c4e338a54ef3f0d3d071 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Fri, 24 Apr 2020 12:33:20 -0600 Subject: [PATCH 2/2] Fix reference clearing in `Sequence.Reset()` Fixes #182 --- src/Nerdbank.Streams/Sequence`1.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Nerdbank.Streams/Sequence`1.cs b/src/Nerdbank.Streams/Sequence`1.cs index e11c088c..161da147 100644 --- a/src/Nerdbank.Streams/Sequence`1.cs +++ b/src/Nerdbank.Streams/Sequence`1.cs @@ -373,7 +373,7 @@ internal void Assign(T[] array) /// internal void ResetMemory(ArrayPool arrayPool) { - this.ClearReferences(this.Start, this.End); + this.ClearReferences(this.Start, this.End - this.Start); this.Memory = default; this.Next = null; this.RunningIndex = 0;