();
_keepalive.Add(thunk);
}
@@ -830,47 +863,15 @@ public void ResetSlots()
return;
}
_alreadyReset = true;
-#if DEBUG
- IntPtr tp_name = Marshal.ReadIntPtr(_type, TypeOffset.tp_name);
- string typeName = Marshal.PtrToStringAnsi(tp_name);
-#endif
- foreach (var offset in _slots.Keys)
- {
- IntPtr ptr = GetDefaultSlot(offset);
-#if DEBUG
- //DebugUtil.Print($"Set slot<{TypeOffsetHelper.GetSlotNameByOffset(offset)}> to 0x{ptr.ToString("X")} at {typeName}<0x{_type}>");
-#endif
- Marshal.WriteIntPtr(_type, offset, ptr);
- }
-
- foreach (var action in _deallocators)
- {
- action();
- }
+ ResetDefaultSlots();
+ InvokeDeallocActions();
+ InvokeCustomResetors();
- foreach (var pair in _customResetors)
+ if (_keepalive != null)
{
- int offset = pair.Key;
- var resetor = pair.Value;
- resetor?.Invoke(_type, offset);
- }
-
- _customResetors.Clear();
- _slots.Clear();
- _keepalive.Clear();
- _deallocators.Clear();
-
- // Custom reset
- IntPtr handlePtr = Marshal.ReadIntPtr(_type, TypeOffset.magic());
- if (handlePtr != IntPtr.Zero)
- {
- GCHandle handle = GCHandle.FromIntPtr(handlePtr);
- if (handle.IsAllocated)
- {
- handle.Free();
- }
- Marshal.WriteIntPtr(_type, TypeOffset.magic(), IntPtr.Zero);
+ _keepalive.Clear();
}
+ ReleaseGCHandle();
}
public static IntPtr GetDefaultSlot(int offset)
@@ -915,6 +916,65 @@ public static IntPtr GetDefaultSlot(int offset)
return Marshal.ReadIntPtr(Runtime.PyTypeType, offset);
}
+
+ private void InvokeCustomResetors()
+ {
+ if (_customResetors == null) return;
+ foreach (var pair in _customResetors)
+ {
+ int offset = pair.Key;
+ var resetor = pair.Value;
+ resetor?.Invoke(_type, offset);
+ }
+ _customResetors.Clear();
+ }
+
+ private void InvokeDeallocActions()
+ {
+ if (_deallocators == null) return;
+ foreach (var action in _deallocators)
+ {
+ action();
+ }
+ _deallocators.Clear();
+ }
+
+ private void ResetDefaultSlots()
+ {
+ if (_slots == null) return;
+#if DEBUG
+ IntPtr tp_name = Marshal.ReadIntPtr(_type, TypeOffset.tp_name);
+ string typeName = Marshal.PtrToStringAnsi(tp_name);
+#endif
+ foreach (var offset in _slots.Keys)
+ {
+ IntPtr ptr = GetDefaultSlot(offset);
+#if DEBUG
+ //DebugUtil.Print($"Set slot<{TypeOffsetHelper.GetSlotNameByOffset(offset)}> to 0x{ptr.ToString("X")} at {typeName}<0x{_type}>");
+#endif
+ Marshal.WriteIntPtr(_type, offset, ptr);
+ }
+ _slots.Clear();
+ }
+
+ private void ReleaseGCHandle()
+ {
+ if (!ManagedType.IsManagedType(_type))
+ {
+ return;
+ }
+ int offset = ObjectOffset.magic(Runtime.PyObject_TYPE(_type));
+ IntPtr handlePtr = Marshal.ReadIntPtr(_type, offset);
+ if (handlePtr != IntPtr.Zero)
+ {
+ GCHandle handle = GCHandle.FromIntPtr(handlePtr);
+ if (handle.IsAllocated)
+ {
+ handle.Free();
+ }
+ Marshal.WriteIntPtr(_type, offset, IntPtr.Zero);
+ }
+ }
}
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