Memory debugging tool for Python/C API?

"Martin v. Löwis" martin at v.loewis.de
Sun Jan 4 18:05:01 EST 2009


> This works, but I'm not sure if PyString...() really makes a new copy of
> the data (ellowing me to use free())

See the documentation:

http://docs.python.org/c-api/string.html#PyString_FromString

# Return a new string object with a *copy* of the string v as value

> Another example (all on the C side of things) is a function where I
> build a dictionary from a set of keys, putting PyNone into each value
> (Py_INCREF()ing PyNone each time). 

Assuming you use PyDict_SetItem(String): you shouldn't INCREF Py_None;
the SetItem will itself incref the value (and decref any old value
that might be already stored under that key).

The only exception where you, as a caller, have to INCREF is when the
reference is documented as "stolen"; very few API functions steal
references.

> At another point some of the values
> are replaced by other PyObjects. At first I used PyDECREF() on each
> value before setting the new value, but that botched up the dictionary
> beyond repair. Without the PyDECREF() calls everything worked fine.

See above.

> Now to my actual question: Is there some sort of debugging tool that I
> could use to analyze my code to see if everything gets properly
> allocated and deallocated and if the reference counting works right?

You should compile Python in pydebug mode; this will perform some
additional checks each time. In addition, you can look at
sys.getrefcount repeatedly. If you create a test that allocates stuff
and then releases it, then sys.getrefcount should be unchanged. If
it does change, you might have a reference leak.

Regards,
Martin



More information about the Python-list mailing list