GC and extension types

Neil Schemenauer nas at arctrix.com
Sun Mar 4 02:02:51 EST 2001


On Sun, Mar 04, 2001 at 03:35:43AM +0000, David Abrahams wrote:
> Is there documentation someplace on what I must do to make my extension
> types play nicely with GC? I am worried that if I store a pointer to a
> PyObject* in some arbitrary memory (and increment its reference count, of
> course!), the collector might decide it was unreachable anyway and collect
> it.

Relax, it won't.  The GC only breaks reference cycles it can
find.  It actually doesn't even free memory itself.

> From looking at the source code, I have an inkling that it might try to
> account for all the references in a cycle, so that objects will only be
> collected when all known references can be accounted for by the (reachable)
> cycles in which it participates. I guess this would create the opposite but
> more benign problem of being too conservative.

Yes, it errors on the conservative side.  If you extension type
can create reference cycles you should tell the GC how to follow
the references it keeps.  Its easy to do but not well documented
right now.  This may help:

    http://python.ca/nas/python/gc_api.txt

> Anyway, I'm hoping there is authoritative text somewhere that describes
> what's really going on... and that the text isn't just 'C' code!

The C code is authoritative.  Anything else is probably
inaccurate.  :-)

  Neil




More information about the Python-list mailing list