Skip to content
Merged
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
To fix memory access exception when iteration breaks in the middle of…
… the list before reaching end.
  • Loading branch information
JakeJP committed Nov 7, 2023
commit 0a8403181210d26e12974e5dc8d02e349847045e
22 changes: 13 additions & 9 deletions src/runtime/CollectionWrappers/IterableWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@ public IEnumerator<T> GetEnumerator()
{
iterObject = PyIter.GetIter(pyObject);
}

using var _ = iterObject;
while (true)
try
{
using var GIL = Py.GIL();

if (!iterObject.MoveNext())
while (true)
{
iterObject.Dispose();
break;
using var _ = Py.GIL();
if (!iterObject.MoveNext())
{
break;
}
yield return iterObject.Current.As<T>()!;
}
yield return iterObject.Current.As<T>()!;
}
finally
{
using var _ = Py.GIL();
iterObject.Dispose();
}
}
}
Expand Down