Skip to content

Commit ea27b25

Browse files
Decref the type when freeing the instance.
1 parent 4f783fa commit ea27b25

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

Modules/_xxsubinterpretersmodule.c

+14-5
Original file line numberDiff line numberDiff line change
@@ -1759,11 +1759,20 @@ _channelid_new(PyObject *mod, PyTypeObject *cls,
17591759
}
17601760

17611761
static void
1762-
channelid_dealloc(PyObject *v)
1763-
{
1764-
int64_t cid = ((channelid *)v)->id;
1765-
_channels *channels = ((channelid *)v)->channels;
1766-
Py_TYPE(v)->tp_free(v);
1762+
channelid_dealloc(PyObject *self)
1763+
{
1764+
int64_t cid = ((channelid *)self)->id;
1765+
_channels *channels = ((channelid *)self)->channels;
1766+
1767+
PyTypeObject *tp = Py_TYPE(self);
1768+
tp->tp_free(self);
1769+
/* "Instances of heap-allocated types hold a reference to their type."
1770+
* See: https://docs.python.org/3.11/howto/isolating-extensions.html#garbage-collection-protocol
1771+
* See: https://docs.python.org/3.11/c-api/typeobj.html#c.PyTypeObject.tp_traverse
1772+
*/
1773+
// XXX Why don't we implement Py_TPFLAGS_HAVE_GC, e.g. Py_tp_traverse,
1774+
// like we do for _abc._abc_data?
1775+
Py_DECREF(tp);
17671776

17681777
_channels_drop_id_object(channels, cid);
17691778
}

0 commit comments

Comments
 (0)