Python mem leaks?

T

tomas.bouda

Hi,
I'm facing a problem which seems to be a Python bug...

I've got an application written in C and having Python embedded inside.
The situation is the following:
1) C object is wrapped by PyCObject and put into Python function
2) The PyCObject has given a finalizer which frees the C object inside
3) The Python function "do_smth(panel)" is called from C

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!

Any comments or proposals?

Thanks a lot, Tobbi
 
D

Duncan Booth

(e-mail address removed) wrote in

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.
 
T

tomas.bouda

Hi,
you're right, my objects were held in Python backtrace.
That was what I thought but I was a bit confused 'cause PyErr_Clear() didn't
clear the backtrace, just set the exception flag.

Now, I'm calling { PyErr_SetNone(PyExc_Exception); PyErr_Clear(); } which
clears memory. Probably a hack but works fine.

Thanks a lot for your help.

Best Regards,
Tobbi


A.T.Hofkamp writes:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top