10000 fixed Python derived types crashing on shutdown of Python process · pythonnet/pythonnet@9010aa4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9010aa4

Browse files
committed
fixed Python derived types crashing on shutdown of Python process
clearing GCHandle from an instance of Python derived type would drop the last reference to it, so it was destroyed without being removed from reflectedObjects collection
1 parent 91d7081 commit 9010aa4

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/runtime/extensiontype.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,17 @@ public unsafe static void tp_dealloc(NewReference lastRef)
8080

8181
tp_clear(lastRef.Borrow());
8282

83-
bool deleted = loadedExtensions.Remove(lastRef.DangerousGetAddress());
84-
Debug.Assert(deleted);
85-
8683
// we must decref our type: https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_dealloc
8784
DecrefTypeAndFree(lastRef.Steal());
8885
}
8986

9087
public static int tp_clear(BorrowedReference ob)
9188
{
92-
TryFreeGCHandle(ob);
89+
if (TryFreeGCHandle(ob))
90+
{
91+
bool deleted = loadedExtensions.Remove(ob.DangerousGetAddress());
92+
Debug.Assert(deleted);
93+
}
9394

9495
int res = ClassBase.BaseUnmanagedClear(ob);
9596
return res;

src/runtime/runtime.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ static bool TryCollectingGarbage(int runs, bool forceBreakLoops)
339339
else if (forceBreakLoops)
340340
{
341341
NullGCHandles(CLRObject.reflectedObjects);
342+
CLRObject.reflectedObjects.Clear();
342343
}
343344
}
344345
return false;

0 commit comments

Comments
 (0)
0