Skip to content

Commit 8045b5f

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 ab6b69a commit 8045b5f

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy