| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -84,6 +84,11 @@ internal ClassDerivedObject(Type tp) : base(tp) | |||
| 84 | 84 | oldHandle.Free(); | |
| 85 | 85 | } | |
| 86 | 86 | ||
| 87 | + /// <summary> | ||
| 88 | + /// No-op clear. Real cleanup happens in <seealso cref="Finalize(IntPtr)"/> | ||
| 89 | + /// </summary> | ||
| 90 | + public new static int tp_clear(BorrowedReference ob) => 0; | ||
| 91 | + | ||
| 87 | 92 | /// <summary> | |
| 88 | 93 | /// Called from Converter.ToPython for types that are python subclasses of managed types. | |
| 89 | 94 | /// The referenced python object is returned instead of a new wrapper. | |
@@ -854,6 +859,21 @@ public static void PyFinalize(IPythonDerivedType obj) | |||
| 854 | 859 | Finalizer.Instance.AddDerivedFinalizedObject(ref self.RawObj, self.Run); | |
| 855 | 860 | } | |
| 856 | 861 | ||
| 862 | + internal static void Finalize(IntPtr derived) | ||
| 863 | + { | ||
| 864 | + bool deleted = CLRObject.reflectedObjects.Remove(derived); | ||
| 865 | + Debug.Assert(deleted); | ||
| 866 | + | ||
| 867 | + var @ref = NewReference.DangerousFromPointer(derived); | ||
| 868 | + | ||
| 869 | + ClassBase.tp_clear(@ref.Borrow()); | ||
| 870 | + | ||
| 871 | + // rare case when it's needed | ||
| 872 | + // matches correspdonging PyObject_GC_UnTrack | ||
| 873 | + // in ClassDerivedObject.tp_dealloc | ||
| 874 | + Runtime.PyObject_GC_Del(@ref.Steal()); | ||
| 875 | + } | ||
| 876 | + | ||
| 857 | 877 | internal static FieldInfo? GetPyObjField(Type type) => type.GetField(PyObjName, PyObjFlags); | |
| 858 | 878 | ||
| 859 | 879 | internal static UnsafeReferenceWithRun GetPyObj(IPythonDerivedType obj) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -236,18 +236,11 @@ internal nint DisposeAll() | |||
| 236 | 236 | continue; | |
| 237 | 237 | } | |
| 238 | 238 | ||
| 239 | - var @ref = NewReference.DangerousFromPointer(derived.PyObj); | ||
| 240 | - GCHandle gcHandle = ManagedType.GetGCHandle(@ref.Borrow()); | ||
| 241 | - | ||
| 242 | - bool deleted = CLRObject.reflectedObjects.Remove(derived.PyObj); | ||
| 243 | - Debug.Assert(deleted); | ||
| 244 | - // rare case when it's needed | ||
| 245 | - // matches correspdonging PyObject_GC_UnTrack | ||
| 246 | - // in ClassDerivedObject.tp_dealloc | ||
| 247 | - Runtime.PyObject_GC_Del(@ref.Steal()); | ||
| 248 | - collected++; | ||
| 239 | + #pragma warning disable CS0618 // Type or member is obsolete. OK for internal use | ||
| 240 | + PythonDerivedType.Finalize(derived.PyObj); | ||
| 241 | + #pragma warning restore CS0618 // Type or member is obsolete | ||
| 249 | 242 | ||
| 250 | - gcHandle.Free(); | ||
| 243 | + collected++; | ||
| 251 | 244 | } | |
| 252 | 245 | } | |
| 253 | 246 | finally | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -220,7 +220,8 @@ internal static bool TryFreeGCHandle(BorrowedReference reflectedClrObject, Borro | |||
| 220 | 220 | IntPtr raw = Util.ReadIntPtr(reflectedClrObject, offset); | |
| 221 | 221 | if (raw == IntPtr.Zero) return false; | |
| 222 | 222 | ||
| 223 | - ((GCHandle)raw).Free(); | ||
| 223 | + var handle = (GCHandle)raw; | ||
| 224 | + handle.Free(); | ||
| 224 | 225 | ||
| 225 | 226 | Util.WriteIntPtr(reflectedClrObject, offset, IntPtr.Zero); | |
| 226 | 227 | return true; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments