How properly manage memory of this PyObject* array?? (C extension)

David Bolen db3l at fitlinxx.com
Wed Jul 19 13:39:55 EDT 2006


"seberino at spawar.navy.mil" <seberino at spawar.navy.mil> writes:

> > *WRONG*. The object exists in and of itself. There may be one *or more*
> > references to it, via pointers, scattered about in memory; they are
> > *NOT* components of the object. A reference count is maintained inside
> > the object and manipulated by Py_INCREF etc. The Python garbage
> > collector knows *nothing* about the memory occupied by those pointers;
> 
> John - Thanks again,
> I think I just learned something that is important.  If I free memory
> associated
> with pointers to Python objects it will NOT erase the Python objects!!!
> I can merrily follow your orders to free(my_array); without worrying
> about
> nuking the Python objects too early!  Thanks for pointing out that
> reference count is maintained inside the Python object itself.

This may be clear from the thread, but since you don't mention it, you
must also have issued the Py_DECREF for each object reference in
my_array at the point that you're planning on freeing it.

Otherwise, while you will have freed up your own local memory
allocation and no longer make use of the object references (pointers)
previously there, the Python memory manager doesn't know that - it
still has a ref count for your prior references - and the objects
themselves will never be freed.

-- David



More information about the Python-list mailing list