Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eef67db

Browse files
JakeJPlostmsu
authored andcommitted
To fix memory access exception when iteration breaks in the middle of the list before reaching end.
1 parent 8e8c3f3 commit eef67db

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/runtime/CollectionWrappers/IterableWrapper.cs

+13-9
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,22 @@ public IEnumerator<T> GetEnumerator()
2424
{
2525
iterObject = PyIter.GetIter(pyObject);
2626
}
27-
28-
using var _ = iterObject;
29-
while (true)
27+
try
3028
{
31-
using var GIL = Py.GIL();
32-
33-
if (!iterObject.MoveNext())
29+
while (true)
3430
{
35-
iterObject.Dispose();
36-
break;
31+
using var _ = Py.GIL();
32+
if (!iterObject.MoveNext())
33+
{
34+
break;
35+
}
36+
yield return iterObject.Current.As<T>()!;
3737
}
38-
yield return iterObject.Current.As<T>()!;
38+
}
39+
finally
40+
{
41+
using var _ = Py.GIL();
42+
iterObject.Dispose();
3943
}
4044
}
4145
}

0 commit comments

Comments
 (0)