[Patches] Re: Garbage collection patches for Python

Guido van Rossum guido@python.org
Tue, 29 Feb 2000 11:43:38 -0500


I'm perusing the malloc and gc threads in more depth...

Just responding to one misleading issue:

[Vladimir]
> > Every chunk of memory must be manupulated via the same malloc family.

[Neil]
> Yes, and this is where things get tricky.  Extension modules can
> use malloc to allocate objects and pass them to the Python core.
> Python uses PyMem_FREE or similar and *boom*, memory corruption
> (if they are different mallocs).

Note that for object allocation, this is not true.  Py_DECREF never
deallocates the object directly -- it calls the function stored as
tp_dealloc in the object's type object.  Thus, the extension that
defines the object type always gets control back to deallocate the
object.

Of course, for chunks of memory that aren't objects, all the above
precautions hold.

Note that e.g. Tcl is also careful with this.  Its defines memory
allocation and deallocation APIs: Tcl_Alloc, Tcl_Free, Tcl_Realloc.
It has a few places where a routine returns a chunk of memory
allocated by Tcl that should be freed by the caller, but the caller
should always do this using Tcl_Free.  (This is very important on
Windows, where the Tcl/Tk DLL uses a different allocator than Python!)

I'm not aware of Python APIs that have such a problem (since they
almost all exclusively deal with objects) but if there are any, I
presume that Vladimir's patch set will fix them...

--Guido van Rossum (home page: http://www.python.org/~guido/)