[Zope-Coders] Python 2.2 and Zope 2.6

Tim Peters tim@zope.com
Wed, 29 May 2002 23:42:11 -0400


[Guido]
> ...
> More work, but perhaps leading to the cause, could you try with "2.3"
> (== current CVS), pymalloc enabled (which it is by default in 2.3), in
> debug mode, and with the symbol PYMALLOC_DEBUG defined manually?  That
> enables heavy malloc debugging which often points to the culprit.

Building in debug mode automatically defines PYMALLOC_DEBUG in 2.3/CVS
Python, so it's just a matter of doing a debug build.

When you do, Python wraps *all* calls to all PyMem_XYZ and PyObject_XYZ
memory allocation, reallocation and deallocation functions (everything
except direct calls to the platform malloc/realloc/calloc/free).  It inserts
"pad bytes" with a special pattern on both ends of each memory block handed
out then, fills newly allocated memory with a second special pattern, and
fills newly freed memory with a third special pattern.  These bit patterns
are contrived so as to most likely be invalid when viewed as addresses,
ASCII strings, or floating-point numbers, so use of uninitialized or freed
memory is likely to cause a gross program failure.  When realloc'ing and
free'ing, the debug mode wrappers also check the pad bytes on each end to
ensure nobody has scribbled over them.  Unlike, say, efence, this is
effective at catching even one-byte overwrites and underwrites, although it
doesn't catch them until the memory is freed.

Etc -- there are other gimmicks.  If it catches a problem, you'll quickly
discover them <wink>.  IIRC, there have been 3 memory-corruption problems in
CVS Python since the debug-mode pymalloc was introduced, and it provided
solid evidence to help track down all of them (twice it caught bad code
scribbling over the pad bytes "off the ends"; and once it caught code trying
to use freed memory, by provoking a memory fault when the code tried to use
a "dead byte" bit pattern as an address).

> ...
> (It may not work in debug mode, but you can use PYMALLOC_DEBUG even
> outside debug mode.

I'd stick to debug mode, if possible.  Nobody routinely tests PYMALLOC_DEBUG
in a release build (& I haven't tried it in months).

> I vaguely remember that I couldn't get 3rd party extension linkage
> working in debug mode, but I'm not sure about this.)

I haven't seen a problem there yet, but haven't tried either <wink>.