C extension reference count confusion (always)

Harald Kirsch kirschh at lionbioscience.com
Wed Aug 8 03:04:49 EDT 2001


"Bob Greschke" <bob at passcal.nmt.edu> writes:

> - OK. I have a Python program that call a C extension function.
> - The C function malloc()s a large chunk of memory for use as
> an integer array.
> - The C function then uses the NumPy function
> PyArray_FromDimsAndData()
> to create a NumPy array using the malloc()ed memory as its first
> address,
> and then calls another C function to fill up the malloc()ed/NumPy
> array with data.
> - The extension function then returns the NumPy array.  This all seems
> to work fine.

I don't have easy access to the docs of NumPy currently. But you can
check yourself: does PyArray_FromDimsAndData() 

a) gobble your malloc()ed chunk of mem to keep it in its internal
structure 
  OR
b) copies data out of it into its very own allocated memory?

From what you write, I assume (a) is true,
i.e. PyArray_FromDimsAndData() creates a fresh Python object (mostly)
from the memory you prepared. That means you no longer have to care
for the malloced chunk but rather for the Python object, but...

That Python object, as I understand, is returned by the C extension
function to the Python machinery which called that function. That is,
the extension function gives away its (only counted) reference to the
Python machinery. Consequently the Python machinery is now responsible
for deallocating it eventually.

In your Python code you probably assign the result of calling the
extension function to some variable. As soon as this variable --- and
any others you might have assigned the value to in the meantime ---
goes out of scope, the array will be destroyed. This happens, for
example, when leaving the current function.

  Harald Kirsch

-- 
----------------+------------------------------------------------------
Harald Kirsch   | kirschh at lionbioscience.com | "How old is the epsilon?"
LION bioscience | +49 6221 4038 172          |        -- Paul Erdös
       *** Please do not send me copies of your posts. ***



More information about the Python-list mailing list