473,409 Members | 1,983 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,409 software developers and data experts.

Py_DECREF Question:

When I add an object created locally to a mapping or sequence (that will
be returned from a function into an running instance of the Python
interpreter), I need to call Py_DECREF on the object, right?
Unfortunately, I really feel like the following code leaks memory...

--- for example ---

PyObject* list = Py_Buildvalue("[]");

while(go) {
PyObject* num = Py_BuildValue("i", 10);

PyList_Append(list, num);

Py_DECREF(num);
}

return list;

/* The responsibility of decrementing the list now lies within the scope
of Python. */

--- end example ---

Secondly, __and most importantly__, does anyone have any tools and/or
recommendations for detecting memory leaks when writing extension
modules in C? I've been using printf() and showing the OBJECT->ob_refcnt
(which, is always 1 when I'm "done" with the pointer?) but this seems
fairly newb.

I'm mostly asking so I can cleanup any craziness in my code before I try
and release pyiw (the Python bindings for Wireless Networking in Linux).
I really want it to be as clean as possible before showing anyone
else. :)

Aug 15 '05 #1
2 5328
Jeremy Moles wrote:
When I add an object created locally to a mapping or sequence ...
I need to call Py_DECREF on the object, right? ... for example:
PyObject* list = Py_Buildvalue("[]");
...
PyObject* num = Py_BuildValue("i", 10);
PyList_Append(list, num);
...

... does anyone have any tools and/or recommendations for detecting
memory leaks when writing extension modules in C? ...


Here's how to answer this for yourself:
PyObject* list = Py_Buildvalue("[]");
int precount;
...
PyObject* num = Py_BuildValue("i", 10);
precount = num.ob_refcnt;
PyList_Append(list, num);
<here compare precount and num.ob_refcnt>
...

If you need to do the DECREF, precount should exceed num.ob_refcnt.
If they match, the "PyList_Append" call "stole" your reference.

--Scott David Daniels
Sc***********@Acm.Org
Aug 15 '05 #2
[Jeremy Moles]
When I add an object created locally to a mapping or sequence (that will
be returned from a function into an running instance of the Python
interpreter), I need to call Py_DECREF on the object, right?
It depends on the the function or macro, but usually the answer is Yes.

The C API docs always tell you when a function or macro steals a
reference. For instance, the docs for PyList_SET_ITEM specifically
mention reference theft. If the docs don't say anything, then it means
that the call does not affect the ref count of the function parameters.
For instance, the docs for PyList_Append() do not say anything about
ref counts:

http://docs.python.org/api/listObjects.html

This is easy to verify by looking at the source in
Objects/listobject.c:

int n = PyList_GET_SIZE(self);

assert (v != NULL);
if (n == INT_MAX) {
PyErr_SetString(PyExc_OverflowError,
"cannot add more objects to list");
return -1;
}

if (list_resize(self, n+1) == -1)
return -1;

Py_INCREF(v);
PyList_SET_ITEM(self, n, v);
return 0;

The presence of the INCREF confirms that PyList_Append does not steal
your reference.
Secondly, __and most importantly__, does anyone have any tools and/or
recommendations for detecting memory leaks when writing extension
modules in C?
After about two days of coding with the C API, you just get good at
reading the code and being able to track when you've added or consumed
a reference. That desk checking takes care of 95% of the problems.

For the remaining 5%, I run a test suite multiple times in the debug
mode, do a gc.collect(), and print sys.gettotalrefcount() after each
run. For an example, see the last few lines of Lib/test/test_set.py.
If the counts become constant after the first couple of runs, it is a
good bet that your extension doesn't leak. Of course, it helps if your
test suite is thorough and exercises all code paths.
I'm mostly asking so I can cleanup any craziness in my code before I try
and release pyiw (the Python bindings for Wireless Networking in Linux).
I really want it to be as clean as possible before showing anyone
else. :)


I'm sure your users will appreciate the devotion to quality.

Raymond

Aug 15 '05 #3

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

Similar topics

3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
0
by: Hrvoje =?UTF-8?Q?Nik=C5=A1i=C4=87?= | last post by:
I'm wondering what happens with the exception info during object cleanup immediately after an exception is thrown. Consider this code: PyObject *args = PyBuild_Value("(O(O){})", name, parent);...
1
by: Kevin D. Smith | last post by:
I'm trying to use Python from another application that allows me to load functions from a DLL. While I have most of the API working, I have one big problem: Py_DECREF and PyINCREF are macros which...
1
by: yuri.feldman | last post by:
Hello, I'm embedding Python interpreter in a Win32 console application. I use C++. I would like to use the WinAPI LoadLibrary function to load the python dll at runtime (followed by...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.