Q: memory ownership in python extensions

Frank Stajano fstajano at uk.research.att.com
Tue Jun 27 13:34:25 EDT 2000


This is a very basic question from someone who has been using Python
for quite a while but only just started writing an extension module.

In the C code, I malloc some memory, perform some processing that
writes a string in it, then turn this C string into a Python string
via Py_BuildValue. The question is: Who is responsible for freeing
that memory? Example code fragment follows.

             ...
	ciphertext = (byte*) malloc(pLen+1); /* who frees this? */
	if (!ciphertext) {
		PyErr_NoMemory();
		return NULL;
	}
	encrypt_block(key, kLen, plaintext, pLen, ciphertext);
	return Py_BuildValue("s#", ciphertext, pLen);

It would be logical and symmetrical for the code who malloc'ed to be
the one responsible for free'ing. For this to happen, Py_BuildValue
would have to take a copy. But I strongly suspect that, for efficiency
reasons, Py_BuildValue does not take a copy and just reuses the memory
passed to it; in which case the free would be done by the Python
system when the reference count of the Python object goes to 0.

Which one of these is the actual behaviour of Py_BuildValue is
something that isn't at all apparent from Chapter 1.9 of "Extending
and embedding" version 1.5.2 (hint hint). I also couldn't find any
reference to this issue in the FAQ by searching for malloc or
Py_BuildValue. Feel free to embarrass me by pointing me at the bit I
missed...




More information about the Python-list mailing list