Skip to content

Commit 7271d88

Browse files
committed
all Dealloc overrides simply duplicate Clear, so just call both from tp_dealloc and don't override Dealloc
1 parent 5bca333 commit 7271d88

8 files changed

+19
-72
lines changed

src/runtime/constructorbinding.cs

-12
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,6 @@ public static IntPtr tp_repr(IntPtr ob)
149149
return self.repr;
150150
}
151151

152-
protected override void Dealloc()
153-
{
154-
Runtime.Py_CLEAR(ref this.repr);
155-
base.Dealloc();
156-
}
157-
158152
protected override void Clear()
159153
{
160154
Runtime.Py_CLEAR(ref this.repr);
@@ -247,12 +241,6 @@ public static IntPtr tp_repr(IntPtr ob)
247241
return self.repr;
248242
}
249243

250-
protected override void Dealloc()
251-
{
252-
Runtime.Py_CLEAR(ref this.repr);
253-
base.Dealloc();
254-
}
255-
256244
protected override void Clear()
257245
{
258246
Runtime.Py_CLEAR(ref this.repr);

src/runtime/eventbinding.cs

-6
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,6 @@ public static IntPtr tp_repr(IntPtr ob)
103103
return Runtime.PyString_FromString(s);
104104
}
105105

106-
protected override void Dealloc()
107-
{
108-
Runtime.Py_CLEAR(ref this.target);
109-
base.Dealloc();
110-
}
111-
112106
protected override void Clear()
113107
{
114108
Runtime.Py_CLEAR(ref this.target);

src/runtime/eventobject.cs

-10
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,6 @@ public static IntPtr tp_repr(IntPtr ob)
198198
}
199199

200200

201-
protected override void Dealloc()
202-
{
203-
if (this.unbound is not null)
204-
{
205-
Runtime.XDecref(this.unbound.pyHandle);
206-
this.unbound = null;
207-
}
208-
base.Dealloc();
209-
}
210-
211201
protected override void Clear()
212202
{
213203
if (this.unbound is not null)

src/runtime/extensiontype.cs

+12-3
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,22 @@ void SetupGc ()
5656

5757
protected virtual void Dealloc()
5858
{
59-
ClearObjectDict(this.pyHandle);
59+
var type = Runtime.PyObject_TYPE(this.ObjectReference);
6060
Runtime.PyObject_GC_Del(this.pyHandle);
61-
// Not necessary for decref of `tpHandle`.
61+
// Not necessary for decref of `tpHandle` - it is borrowed
62+
6263
this.FreeGCHandle();
64+
65+
// we must decref our type: https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_dealloc
66+
Runtime.XDecref(type.DangerousGetAddress());
6367
}
6468

6569
/// <summary>DecRefs and nulls any fields pointing back to Python</summary>
66-
protected virtual void Clear() { }
70+
protected virtual void Clear()
71+
{
72+
ClearObjectDict(this.pyHandle);
73+
// Not necessary for decref of `tpHandle` - it is borrowed
74+
}
6775

6876
/// <summary>
6977
/// Type __setattr__ implementation.
@@ -96,6 +104,7 @@ public static void tp_dealloc(IntPtr ob)
96104
// Clean up a Python instance of this extension type. This
97105
// frees the allocated Python object and decrefs the type.
98106
var self = (ExtensionType)GetManagedObject(ob);
107+
self?.Clear();
99108
self?.Dealloc();
100109
}
101110

src/runtime/methodbinding.cs

+2-13
Original file line numberDiff line numberDiff line change
@@ -229,21 +229,10 @@ public static IntPtr tp_repr(IntPtr ob)
229229
return Runtime.PyString_FromString($"<{type} method '{name}'>");
230230
}
231231

232-
private void ClearMembers()
233-
{
234-
Runtime.Py_CLEAR(ref target);
235-
Runtime.Py_CLEAR(ref targetType);
236-
}
237-
238-
protected override void Dealloc()
239-
{
240-
this.ClearMembers();
241-
base.Dealloc();
242-
}
243-
244232
protected override void Clear()
245233
{
246-
this.ClearMembers();
234+
Runtime.Py_CLEAR(ref this.target);
235+
Runtime.Py_CLEAR(ref this.targetType);
247236
base.Clear();
248237
}
249238

src/runtime/methodobject.cs

+5-16
Original file line numberDiff line numberDiff line change
@@ -200,26 +200,15 @@ public static IntPtr tp_repr(IntPtr ob)
200200
return Runtime.PyString_FromString($"<method '{self.name}'>");
201201
}
202202

203-
private void ClearMembers()
203+
protected override void Clear()
204204
{
205-
Runtime.Py_CLEAR(ref doc);
206-
if (unbound != null)
205+
Runtime.Py_CLEAR(ref this.doc);
206+
if (this.unbound != null)
207207
{
208-
Runtime.XDecref(unbound.pyHandle);
209-
unbound = null;
208+
Runtime.XDecref(this.unbound.pyHandle);
209+
this.unbound = null;
210210
}
211-
}
212211

213-
protected override void Dealloc()
214-
{
215-
this.ClearMembers();
216-
ClearObjectDict(this.pyHandle);
217-
base.Dealloc();
218-
}
219-
220-
protected override void Clear()
221-
{
222-
this.ClearMembers();
223212
ClearObjectDict(this.pyHandle);
224213
base.Clear();
225214
}

src/runtime/moduleobject.cs

-6
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,6 @@ public static int tp_traverse(IntPtr ob, IntPtr visit, IntPtr arg)
308308
return 0;
309309
}
310310

311-
protected override void Dealloc()
312-
{
313-
tp_clear(this.pyHandle);
314-
base.Dealloc();
315-
}
316-
317311
protected override void Clear()
318312
{
319313
Runtime.Py_CLEAR(ref this.dict);

src/runtime/overload.cs

-6
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,6 @@ public static IntPtr tp_repr(IntPtr op)
5858
return doc;
5959
}
6060

61-
protected override void Dealloc()
62-
{
63-
Runtime.Py_CLEAR(ref this.target);
64-
base.Dealloc();
65-
}
66-
6761
protected override void Clear()
6862
{
6963
Runtime.Py_CLEAR(ref this.target);

0 commit comments

Comments
 (0)