[Python-Dev] PyMem_Malloc() vs PyObject_Malloc() (original) (raw)

Nick Coghlan ncoghlan at gmail.com
Fri Sep 4 11:45:08 CEST 2009


Kristján Valur Jónsson wrote:

My feeling Is that these two APIs shouldn’t be interchangeable. Especially since you can’t hand a PyObjectMalloc’d object to PyMemFree() so the inverse shouldn’t be expected to work.

I thought this had officially been deemed illegal for a while, and Google found the reference I was looking for in the What's New for 2.5:

"""Previously these different families all reduced to the platform's malloc() and free() functions. This meant it didn't matter if you got things wrong and allocated memory with the PyMem function but freed it with the PyObject function. With 2.5's changes to obmalloc, these families now do different things and mismatches will probably result in a segfault. You should carefully test your C extension modules with Python 2.5."""

So either the allocation or the release needs to change here.

The behaviour of PyObject_Del when handed a pointer it doesn't recognise is currently undocumented. It may be best to make it officially undefined in order to further discourage people from relying on the implicit delegation to PyMem_Free.

Cheers, Nick.

-- Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia



More information about the Python-Dev mailing list