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

John Machin sjmachin at lexicon.net
Mon Jul 10 17:42:31 EDT 2006


On 11/07/2006 4:39 AM, seberino at spawar.navy.mil wrote:
>>> Q2: Must I do free(my_array); at end of function??
>> unless some other part of your program holds on to it
> 
> F.
> 
> Thanks!  If I understand you correctly then I never have
> to do free(my_array); because all the elements
> of my_array are still being used and appended to other
> structures elsewhere right?

*WRONG* -- reread /F's response:
"""
unless some other part of your program holds on to it, of course you 
have to release it.  it's a bit surprising that you have to ask this, 
really -- any C tutorial should explain how malloc/free works.
"""

> 
> As long as each element of my_array is managed
> properly and freed properly there is  NEVER any
> reason to worry about fact that free(my_array)
> will never get run right?

*WRONG* -- the reason to worry is that the memory occupied by the 
my_array itself is never handed back. If you don't free it, repeated 
calls to your function will cause your app to run out of memory.

It's quite simple, really: You malloc it, you free it. I share the 
effbot's surprise: If you (or the taxpayers!) paid money for a 
course/book/tutorial that didn't include that advice, a claim for a 
refund is definitely indicated.

HTH,
John



More information about the Python-list mailing list