Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure LINQ operators should not dispose more than once #581

Merged
merged 4 commits into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion MoreLinq.Test/TestingSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ public IEnumerator<T> GetEnumerator()
Assert.That(_sequence, Is.Not.Null, "LINQ operators should not enumerate a sequence more than once.");
var enumerator = _sequence.GetEnumerator().AsWatchtable();
_disposed = false;
enumerator.Disposed += delegate { _disposed = true; };
enumerator.Disposed += delegate
{
Assert.That(_disposed, Is.False, "LINQ operators should not dispose a sequence more than once.");
_disposed = true;
};
enumerator.MoveNextCalled += delegate { MoveNextCallCount++; };
_sequence = null;
return enumerator;
Expand Down
1 change: 1 addition & 0 deletions MoreLinq/Flatten.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public static IEnumerable<object> Flatten(this IEnumerable source, Func<object,
}

(e as IDisposable)?.Dispose();
e = null;
}
}
finally
Expand Down
25 changes: 17 additions & 8 deletions MoreLinq/ZipImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@ static IEnumerable<TResult> ZipImpl<T1, T2, T3, T4, TResult>(
int limit,
Folder<IEnumerator, Exception> errorSelector = null)
{
IEnumerator<T1> e1;
IEnumerator<T2> e2;
IEnumerator<T3> e3;
IEnumerator<T4> e4;
IEnumerator<T1> e1 = null;
IEnumerator<T2> e2 = null;
IEnumerator<T3> e3 = null;
IEnumerator<T4> e4 = null;
var terminations = 0;

using (e1 = s1 .GetEnumerator())
using (e2 = s2 .GetEnumerator())
using (e3 = s3?.GetEnumerator())
using (e4 = s4?.GetEnumerator())
try
{
e1 = s1 .GetEnumerator();
e2 = s2 .GetEnumerator();
e3 = s3?.GetEnumerator();
e4 = s4?.GetEnumerator();

while (true)
{
var n = 0;
Expand All @@ -59,6 +61,13 @@ static IEnumerable<TResult> ZipImpl<T1, T2, T3, T4, TResult>(
yield break;
}
}
finally
{
e1?.Dispose();
e2?.Dispose();
e3?.Dispose();
e4?.Dispose();
}

T Read<T>(ref IEnumerator<T> e, int n)
{
Expand Down