Python mem leaks?

Duncan Booth duncan at NOSPAMrcp.co.uk
Wed Dec 3 09:14:52 EST 2003


tomas.bouda at systinet.com wrote in
news:mailman.69.1070454775.16879.python-list at python.org: 

> Having the two methods: 
> 
> def do_smth1(panel):
>    pass 
> 
> def do_smth2(panel):
>    raise Exception() 
> 
> Calling do_smth1() will cause returning of Py_None, the PyCObject
> <panel> is freed and the C finalizer (mentioned in no.2) is called.
> Calling do_smth2() will case returning NULL a <panel> is not freed nor
> finalizer is called. At this point I get a leak! 
> 
> Is it my fault? I didn't find anywhere in Python doc what should C app
> to do if such memory isn't freed. Seems more likely to be the Python
> leaking bug! 

It is not your fault, but neither is it a memory leak or a bug in Python.
When your function throws an exception the stack frame is held in case 
whatever handles the exception wants to inspect it. This extends the 
lifetime of all the objects references within the function until the stack 
frame is discarded which is not usually until another exception is thrown.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list