Skip to content

Commit 6b87b38

Browse files
committed
do not attempt to deallocate GCHandle of Python derived type instance from finalizer when it is not allocated (could have been cleared)
1 parent a975b7e commit 6b87b38

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/runtime/classderived.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ internal static Type CreateDerivedType(string name,
257257
Type.EmptyTypes);
258258
ILGenerator il = methodBuilder.GetILGenerator();
259259
il.Emit(OpCodes.Ldarg_0);
260-
il.Emit(OpCodes.Call, typeof(PythonDerivedType).GetMethod("Finalize"));
260+
il.Emit(OpCodes.Call, typeof(PythonDerivedType).GetMethod(nameof(PythonDerivedType.Finalize)));
261261
il.Emit(OpCodes.Ldarg_0);
262262
il.Emit(OpCodes.Call, baseClass.GetMethod("Finalize", BindingFlags.NonPublic | BindingFlags.Instance));
263263
il.Emit(OpCodes.Ret);
@@ -775,8 +775,8 @@ public static T InvokeGetProperty<T>(IPythonDerivedType obj, string propertyName
775775
Runtime.XIncref(self.pyHandle);
776776
using var pyself = new PyObject(self.pyHandle);
777777
PyObject pyvalue = pyself.GetAttr(propertyName);
778-
return (T)pyvalue.AsManagedObject(typeof(T));
779-
}
778+
return (T)pyvalue.AsManagedObject(typeof(T));
779+
}
780780
finally
781781
{
782782
Runtime.PyGILState_Release(gs);
@@ -886,7 +886,7 @@ public static void Finalize(IPythonDerivedType obj)
886886
Runtime.XDecref(dict);
887887
}
888888
Runtime.PyObject_GC_Del(self.pyHandle);
889-
self.gcHandle.Free();
889+
if (self.gcHandle.IsAllocated) self.gcHandle.Free();
890890
}
891891
finally
892892
{

0 commit comments

Comments
 (0)