FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix refcnt error · ayuretsky/pythonnet@fba616a · GitHub

Repository navigation

Commit fba616a

Browse files
committed
Fix refcnt error
1 parent 1ce83fc commit fba616a

4 files changed

Lines changed: 16 additions & 10 deletions

File tree

‎src/runtime/extensiontype.cs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,12 @@ public static void FinalizeObject(ManagedType self)
5151
{
5252
Runtime.PyObject_GC_Del(self.pyHandle);
5353
// Not necessary for decref of `tpHandle`.
54-
self.gcHandle.Free();
54+
self.FreeGCHandle();
5555
}
5656

5757
protected void Dealloc()
5858
{
5959
FinalizeObject(this);
60-
FreeGCHandle();
6160
}
6261

6362
/// <summary>

‎src/runtime/managedtype.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ internal abstract class ManagedType
1919

2020
private static readonly HashSet<ManagedType> _managedObjs = new HashSet<ManagedType>();
2121

22+
internal void IncrRefCount()
23+
{
24+
Runtime.XIncref(pyHandle);
25+
}
26+
2227
internal void DecrRefCount()
2328
{
2429
Runtime.XDecref(pyHandle);

‎src/runtime/moduleobject.cs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,11 @@ public ManagedType GetAttribute(string name, bool guess)
174174
/// </summary>
175175
private void StoreAttribute(string name, ManagedType ob)
176176
{
177-
Runtime.PyDict_SetItemString(dict, name, ob.pyHandle);
177+
if (Runtime.PyDict_SetItemString(dict, name, ob.pyHandle) != 0)
178+
{
179+
throw new PythonException();
180+
}
181+
ob.IncrRefCount();
178182
cache[name] = ob;
179183
}
180184

‎src/runtime/typemanager.cs‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,9 @@ namespace Python.Runtime
1717
/// </summary>
1818
internal class TypeManager
1919
{
20-
private static BindingFlags tbFlags;
20+
private const BindingFlags tbFlags = BindingFlags.Public | BindingFlags.Static;
2121
private static Dictionary<Type, IntPtr> cache;
2222

23-
static TypeManager()
24-
{
25-
tbFlags = BindingFlags.Public | BindingFlags.Static;
26-
}
27-
2823
public static void Reset()
2924
{
3025
cache = new Dictionary<Type, IntPtr>(128);
@@ -74,6 +69,7 @@ internal static IntPtr GetTypeHandle(Type type)
7469

7570

7671
/// <summary>
72+
/// Return value: Borrowed reference.
7773
/// Get the handle of a Python type that reflects the given CLR type.
7874
/// The given ManagedType instance is a managed object that implements
7975
/// the appropriate semantics in Python for the reflected managed type.
@@ -311,7 +307,7 @@ internal static IntPtr CreateSubType(IntPtr py_name, IntPtr py_base_type, IntPtr
311307
// derived class we want the python overrides in there instead if they exist.
312308
IntPtr cls_dict = Marshal.ReadIntPtr(py_type, TypeOffset.tp_dict);
313309
Runtime.PyDict_Update(cls_dict, py_dict);
314-
310+
Runtime.XIncref(py_type);
315311
return py_type;
316312
}
317313
catch (Exception e)
@@ -1075,7 +1071,9 @@ private void ResetSlots()
10751071
foreach (var offset in _slots.Keys)
10761072
{
10771073
IntPtr ptr = GetDefaultSlot(offset);
1074+
#if DEBUG
10781075
//DebugUtil.Print($"Set slot<{TypeOffsetHelper.GetSlotNameByOffset(offset)}> to 0x{ptr.ToString("X")} at {typeName}<0x{_type}>");
1076+
#endif
10791077
Marshal.WriteIntPtr(_type, offset, ptr);
10801078
}
10811079

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL