8000 fixed crash in finalizer of CLR types defined in Python, that survive… · pythonnet/pythonnet@3069285 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3069285

Browse files
lostmsuBadSingleton
authored andcommitted
fixed crash in finalizer of CLR types defined in Python, that survive engine shutdown (#1260)
#1256 #1256 During engine shutdown all links from Python to .NET instances are severed. If an instance of CLR class defined in Python survives the shutdown (for example, a reference is stored in static field) and later gets finalized, it will attempt to severe link again, which is an invalid operation. The fix is to check if the link has already been severed and skip that step during finalization.
1 parent 639236a commit 3069285

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/runtime/classderived.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ public static void Finalize(IPythonDerivedType obj)
858858
{
859859
if (0 == Runtime.Py_IsInitialized() || Runtime.IsFinalizing)
860860
{
861-
self.gcHandle.Free();
861+
if (self.gcHandle.IsAllocated) self.gcHandle.Free();
862862
return;
863863
}
864864
}
@@ -873,7 +873,7 @@ public static void Finalize(IPythonDerivedType obj)
873873
// If python's been terminated then just free the gchandle.
874874
if (0 == Runtime.Py_IsInitialized() || Runtime.IsFinalizing)
875875
{
876-
self.gcHandle.Free();
876+
if (self.gcHandle.IsAllocated) self.gcHandle.Free();
877877
return;
878878
}
879879

0 commit comments

Comments
 (0)
0