summaryrefslogtreecommitdiffstats
path: root/Doc/extending
diff options
context:
space:
mode:
authorSam Gross <colesbury@gmail.com>2021-10-27 19:15:13 (GMT)
committerGitHub <noreply@github.com>2021-10-27 19:15:13 (GMT)
commit35e1ff38ee67ee543d9fcb268c3552c5397f9b3f (patch)
tree4bcb6ac31c2957fb9c848193b37fca294d259ee5 /Doc/extending
parent4776b07d178b2800374f5c15da182f1215756205 (diff)
downloadcpython-35e1ff38ee67ee543d9fcb268c3552c5397f9b3f.zip
cpython-35e1ff38ee67ee543d9fcb268c3552c5397f9b3f.tar.gz
cpython-35e1ff38ee67ee543d9fcb268c3552c5397f9b3f.tar.bz2
bpo-28737: Document when tp_dealloc should call PyObject_GC_UnTrack() (GH-29246)
Objects that support garbage collection ("container" objects) should call PyObject_GC_UnTrack() from their destructors before clearing any fields which may point to other "container" objects.
Diffstat (limited to 'Doc/extending')
-rw-r--r--Doc/extending/newtypes.rst14
1 files changed, 13 insertions, 1 deletions
diff --git a/Doc/extending/newtypes.rst b/Doc/extending/newtypes.rst
index 6e17897..23ec8bc 100644
--- a/Doc/extending/newtypes.rst
+++ b/Doc/extending/newtypes.rst
@@ -73,7 +73,19 @@ function::
newdatatype_dealloc(newdatatypeobject *obj)
{
free(obj->obj_UnderlyingDatatypePtr);
- Py_TYPE(obj)->tp_free(obj);
+ Py_TYPE(obj)->tp_free((PyObject *)obj);
+ }
+
+If your type supports garbage collection, the destructor should call
+:c:func:`PyObject_GC_UnTrack` before clearing any member fields::
+
+ static void
+ newdatatype_dealloc(newdatatypeobject *obj)
+ {
+ PyObject_GC_UnTrack(obj);
+ Py_CLEAR(obj->other_obj);
+ ...
+ Py_TYPE(obj)->tp_free((PyObject *)obj);
}
.. index::