Skip to content

Do not clean tpHandle in ClassBase.tp_clear - it might be used in tp_dealloc #1566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/runtime/classbase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ public static int tp_clear(IntPtr ob)
return baseClearResult;
}
}
if (self is not null) self.tpHandle = IntPtr.Zero;
return 0;
}

Expand Down
21 changes: 19 additions & 2 deletions src/runtime/managedtype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,23 @@ internal enum TrackTypes
internal IntPtr pyHandle; // PyObject *
internal IntPtr tpHandle; // PyType *

internal BorrowedReference ObjectReference => new(pyHandle);
internal BorrowedReference TypeReference => new(tpHandle);
internal BorrowedReference ObjectReference
{
get
{
Debug.Assert(pyHandle != IntPtr.Zero);
return new(pyHandle);
}
}

internal BorrowedReference TypeReference
{
get
{
Debug.Assert(tpHandle != IntPtr.Zero);
return new(tpHandle);
}
}

private static readonly Dictionary<ManagedType, TrackTypes> _managedObjs = new Dictionary<ManagedType, TrackTypes>();

Expand Down Expand Up @@ -310,6 +325,8 @@ internal static void InitGCHandle(BorrowedReference reflectedClrObject, GCHandle

internal static void SetGCHandle(BorrowedReference reflectedClrObject, BorrowedReference type, GCHandle newHandle)
{
Debug.Assert(type != null);
Debug.Assert(reflectedClrObject != null);
Debug.Assert(Runtime.PyObject_TypeCheck(reflectedClrObject, type));

int offset = Marshal.ReadInt32(type.DangerousGetAddress(), Offsets.tp_clr_inst_offset);
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2021,7 +2021,11 @@ internal static bool PyType_Check(IntPtr ob)
internal static void PyType_Modified(BorrowedReference type) => Delegates.PyType_Modified(type);
internal static bool PyType_IsSubtype(BorrowedReference t1, IntPtr ofType)
=> PyType_IsSubtype(t1, new BorrowedReference(ofType));
internal static bool PyType_IsSubtype(BorrowedReference t1, BorrowedReference t2) => Delegates.PyType_IsSubtype(t1, t2);
internal static bool PyType_IsSubtype(BorrowedReference t1, BorrowedReference t2)
{
Debug.Assert(t1 != null && t2 != null);
return Delegates.PyType_IsSubtype(t1, t2);
}

internal static bool PyObject_TypeCheck(IntPtr ob, IntPtr tp)
=> PyObject_TypeCheck(new BorrowedReference(ob), new BorrowedReference(tp));
Expand Down
16 changes: 16 additions & 0 deletions tests/test_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,19 @@ def __init__(self, i, s):
assert len(calls) == 1
assert calls[0][0] == 1
assert calls[0][1] == "foo"

# regression test for https://github.com/pythonnet/pythonnet/issues/1565
def test_can_be_collected_by_gc():
from Python.Test import BaseClass

class Derived(BaseClass):
__namespace__ = 'test_can_be_collected_by_gc'

inst = Derived()
cycle = [inst]
del inst
cycle.append(cycle)
del cycle

import gc
gc.collect()