Cyclic garbage collection and segfaults...

Tim Peters at
Thu Jan 15 02:24:40 CET 2004

[Simon Burton]
> In my extensions python seems to segfault in the GC most of all...

That's quite likely:  gc is the only part of the system that routinely
crawls over every container in existence, so is supremely sensitive to all
manner of common C coding errors (wild stores, buffer overruns,
uninitialized memory, mixing calls to different memory subsystems on a
single object, and failure to follow the GC protocols).

> Just yesterday I removed a malloc that was (why??) causing a segfault.

Well, you certainly can't use the system malloc to obtain memory and expect
Python's garbage collection to manage it -- for gc'ed extension types, the
memory must be gotten via PyObject_GC_New() or PyObject_GC_NewVar(), and you
have to follow all the other rules too.  If you haven't read and understood
the "Supporting Cyclic Garbage Collection" section of the Python/C API
manual, you're going to get lots of segfaults.  But if you follow the rules,
you shouldn't get any.  Remember too that this is C code, not Python code,
so the result of not following a rule is more likely to be a nasty crash
than a friendly exception.

More information about the Python-list mailing list