Skip to content

Clear weakref list when reflected object is destroyed #1758

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 1 commit into from
Apr 9, 2022
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
10 changes: 10 additions & 0 deletions src/embed_tests/TestInstanceWrapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ public void OverloadResolution_UnknownToObject()
}
}

[Test]
public void WeakRefIsNone_AfterObjectIsGone()
{
using var makeref = Py.Import("weakref").GetAttr("ref");
var ub = new UriBuilder().ToPython();
using var weakref = makeref.Invoke(ub);
ub.Dispose();
Assert.IsTrue(weakref.Invoke().IsNone());
}

class Base {}
class Derived: Base { }

Expand Down
2 changes: 2 additions & 0 deletions src/runtime/Runtime.Delegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ static Delegates()
PyObject_RichCompareBool = (delegate* unmanaged[Cdecl]<BorrowedReference, BorrowedReference, int, int>)GetFunctionByName(nameof(PyObject_RichCompareBool), GetUnmanagedDll(_PythonDll));
PyObject_IsInstance = (delegate* unmanaged[Cdecl]<BorrowedReference, BorrowedReference, int>)GetFunctionByName(nameof(PyObject_IsInstance), GetUnmanagedDll(_PythonDll));
PyObject_IsSubclass = (delegate* unmanaged[Cdecl]<BorrowedReference, BorrowedReference, int>)GetFunctionByName(nameof(PyObject_IsSubclass), GetUnmanagedDll(_PythonDll));
PyObject_ClearWeakRefs = (delegate* unmanaged[Cdecl]<BorrowedReference, void>)GetFunctionByName(nameof(PyObject_ClearWeakRefs), GetUnmanagedDll(_PythonDll));
PyCallable_Check = (delegate* unmanaged[Cdecl]<BorrowedReference, int>)GetFunctionByName(nameof(PyCallable_Check), GetUnmanagedDll(_PythonDll));
PyObject_IsTrue = (delegate* unmanaged[Cdecl]<BorrowedReference, int>)GetFunctionByName(nameof(PyObject_IsTrue), GetUnmanagedDll(_PythonDll));
PyObject_Not = (delegate* unmanaged[Cdecl]<BorrowedReference, int>)GetFunctionByName(nameof(PyObject_Not), GetUnmanagedDll(_PythonDll));
Expand Down Expand Up @@ -361,6 +362,7 @@ static Delegates()
internal static delegate* unmanaged[Cdecl]<BorrowedReference, BorrowedReference, int, int> PyObject_RichCompareBool { get; }
internal static delegate* unmanaged[Cdecl]<BorrowedReference, BorrowedReference, int> PyObject_IsInstance { get; }
internal static delegate* unmanaged[Cdecl]<BorrowedReference, BorrowedReference, int> PyObject_IsSubclass { get; }
internal static delegate* unmanaged[Cdecl]<BorrowedReference, void> PyObject_ClearWeakRefs { get; }
internal static delegate* unmanaged[Cdecl]<BorrowedReference, int> PyCallable_Check { get; }
internal static delegate* unmanaged[Cdecl]<BorrowedReference, int> PyObject_IsTrue { get; }
internal static delegate* unmanaged[Cdecl]<BorrowedReference, int> PyObject_Not { get; }
Expand Down
12 changes: 12 additions & 0 deletions src/runtime/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,18 @@ internal static int PyObject_Compare(BorrowedReference value1, BorrowedReference

internal static int PyObject_IsSubclass(BorrowedReference ob, BorrowedReference type) => Delegates.PyObject_IsSubclass(ob, type);

internal static void PyObject_ClearWeakRefs(BorrowedReference ob) => Delegates.PyObject_ClearWeakRefs(ob);

internal static BorrowedReference PyObject_GetWeakRefList(BorrowedReference ob)
{
Debug.Assert(ob != null);
var type = PyObject_TYPE(ob);
int offset = Util.ReadInt32(type, TypeOffset.tp_weaklistoffset);
if (offset == 0) return BorrowedReference.Null;
Debug.Assert(offset > 0);
return Util.ReadRef(ob, offset);
}


internal static int PyCallable_Check(BorrowedReference o) => Delegates.PyCallable_Check(o);

Expand Down
6 changes: 6 additions & 0 deletions src/runtime/Types/ClassBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,12 @@ public static void tp_dealloc(NewReference lastRef)

public static int tp_clear(BorrowedReference ob)
{
var weakrefs = Runtime.PyObject_GetWeakRefList(ob);
if (weakrefs != null)
{
Runtime.PyObject_ClearWeakRefs(ob);
}

if (TryFreeGCHandle(ob))
{
IntPtr addr = ob.DangerousGetAddress();
Expand Down