473,382 Members | 1,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Python mem leaks?

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

Jul 18 '05 #1
2 1407
to*********@systinet.com wrote in
news:ma*************************************@pytho n.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 du****@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?
Jul 18 '05 #2
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:
In comp.lang.python, you wrote:

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!


What happens if you handle the exception?

My guess is that in the stack backtrace, there is a reference to panel.
Since handling the exception may mean printing the stack, Python still
needs the object.
If you handle the exception, I would expect Python to release the stack
trace information, and thus the object.
Albert
--
Unlike popular belief, the .doc format is not an open publically available format.


Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Robby Dermody | last post by:
Hey guys (thus begins a book of a post :), I'm in the process of writing a commercial VoIP call monitoring and recording application suite in python and pyrex. Basically, this software sits in a...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 413 open ( +1) / 3407 closed (+10) / 3820 total (+11) Bugs : 897 open ( -3) / 6167 closed (+18) / 7064 total (+15) RFE : 234 open...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 421 open ( -2) / 3549 closed (+10) / 3970 total ( +8) Bugs : 943 open (-17) / 6471 closed (+25) / 7414 total ( +8) RFE : 260 open...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.