[Python-Dev] A little GC confusion

Martin v. Loewis martin@v.loewis.de
23 Feb 2002 11:51:57 +0100


"David Abrahams" <david.abrahams@rcn.com> writes:

> Nice try, but no cigar I'm afraid: copying the tp_is_gc slot from
> PyType_Type into my metatype before PyType_Ready() doesn't prevent the
> crash.
> 
> Does anyone really understand what happens here?

After studying your code in a debugger, it turns out that the code now
crashes for a different reason: The "AA" class created in make_class
is traversed in subtract_refs. To do so, its type's traverse function
is invoked, i.e. classtype_meta_object.tp_traverse. This is a null
pointer, hence the crash.

If you want objects (in your case, classes) to participate in GC, the
of the objects (in your case, the metaclass) needs to implement the GC
API. IOW, don't set Py_TPFLAGS_HAVE_GC in a type unless you also set
tp_clear and tp_traverse in the same type, see

http://www.python.org/doc/current/api/supporting-cycle-detection.html

for details. This likely has been the problem all the time; if I
remove tp_is_gc, but implement tp_traverse, your test case (import
AA,pdb) does not crash anymore.

BTW, gcc rejects the code you've posted, as you cannot use
PyType_Type.tp_basicsize in an initializer of a global object (it's
not a constant).

HTH,
Martin