Skip to content

Commit 9f04cd3

Browse files
committed
trace2: fix incomplete disposal of writers on cleanup
ReleaseManagedResources iterates forward by index while removing elements from the same list. Each removal shifts remaining elements left, but the loop increments i, causing the next element to be skipped. As a result, only about half of the writers are disposed and removed, leaving file handles or buffers open. Fix by iterating in reverse so that removals do not shift any unvisited indices, and use RemoveAt(i) to avoid a redundant linear search. Signed-off-by: Matthew John Cheetham <mjcheetham@outlook.com>
1 parent e78f0db commit 9f04cd3

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/shared/Core/Trace2.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,11 @@ protected override void ReleaseManagedResources()
460460
{
461461
try
462462
{
463-
for (int i = 0; i < _writers.Count; i += 1)
463+
for (int i = _writers.Count - 1; i >= 0; i--)
464464
{
465-
using (var writer = _writers[i])
465+
using (_writers[i])
466466
{
467-
_writers.Remove(writer);
467+
_writers.RemoveAt(i);
468468
}
469469
}
470470
}

0 commit comments

Comments
 (0)