Memory (mis)management when writing C extensions?

Lawrence E Fitzpatrick (lef@pls.com)
Wed, 25 Jan 1995 21:41:52 GMT

Hi, more memory management questions. In the following dialog,
I am looking at the code for release 1.1.

I am trying to understand the rules for using refcnting when
implementing C extensions to python. The blurb in the extensions
document helps a lot, but there are a few unanswered questions,
especially with regard to what some of the modules on the distribution
do. This is what is (supposed to be) happening I think:

-- a NEWOBJ sets the reference count to 1,
-- most container objects (mapping type, list), perform
an INCREF() on the inserted object at insertion time,
(there is the documented exception for set* methods)

I've looked at the code for dictinsert() and addlistitem() and this
appears to be the case. However, if I search the python/Modules
directory for occurances of dictinsert and addlistitem calls, I find
many cases that appear to be generating unreclaimable objects. For
example:

timemodule.c: dictinsert(d, "timezone", newintobject((long)timezone));
timemodule.c: dictinsert(d, "altzone", newintobject((long)altzone));
timemodule.c: dictinsert(d, "altzone", newintobject((long)timezone-3600));
timemodule.c: dictinsert(d, "daylight", newintobject((long)daylight));
dbmmodule.c:
static object *
dbm_keys(dp, args)
register dbmobject *dp;
object *args;
{
...
for (key = dbm_firstkey(dp->di_dbm); key.dptr;
key = dbm_nextkey(dp->di_dbm) ) {
item = newsizedstringobject(key.dptr, key.dsize);
if ( item == 0 )
return NULL;
--->> addlistitem(v, item);
}
return v;
}

In the timemodule.c examples, aren't all the inserted objects memory
leaks?

In the last example, shouldn't the addlistitem(v,item) be followed by
a DECREF(item), otherwise its a leak?

Is there a consistent policy about what happens to an item that is
inserted but the insertion operation fails? That is, is the
container operation considered atomic with regard to the refcnt
modification? I recall reading something about it somewhere but
cannot find it again (my head is swimming!).

Cheers,
lef