Debugging reference counts in python/C

karmadharma at gmail.com karmadharma at gmail.com
Wed Aug 30 17:32:26 EDT 2006


Hello everybody

I am trying to make sure that my (non trivial) C module is cleaning up
properly after itself, and consequently I've built python with
--py-debug which, besides allowing me to use certain functions, it also
prints the reference count at the end.

I am finding, however, that the reference count on exit seems to
increase by 1 every time I create one of my objects, poring over the
output of the debugging functions seems to imply that  the only thing
that changes is the refcount of

[somenumber] '<dummy key>'

where somenumber seems proportional to the number of objects I create,
if I create n+1 objects, somenumber increases by 1. Looking at the docs
this seems related to empty dictionaries, but I'm really stumped at the
moment.

Out of curiosity I also did this stupid test thinking it would return
the same refcount, but evidently it doesn't:

Type "help", "copyright", "credits" or "license" for more information.
>>> a=1
[17214 refs]
>>> (ctrl-D)
[17214 refs]
[6606 refs]

Type "help", "copyright", "credits" or "license" for more information.
>>> a=1
[17214 refs]
>>> b=2
[17217 refs]
>>> (ctrl-D)
[17217 refs]
[6608 refs]

as you can see just assigning another variable causes the refcount to
remain higher when python exits.

Is this something I should be worried about at all? I am new to writing
python C extensions, and that's why I never really worried about all
this before; apologies if my question is obvious and/or trivial...




More information about the Python-list mailing list