Python C API: How to debug reference leak?

Chris Angelico rosuav at gmail.com
Wed Sep 28 05:22:23 EDT 2016


On Wed, Sep 28, 2016 at 6:56 PM, Gregory Ewing
<greg.ewing at canterbury.ac.nz> wrote:
> dieter wrote:
>>
>> dl l <ldlchina at gmail.com> writes:
>>
>>> When I debug in C++, I see the reference count of a PyObject is 1.
>
>>> How can I find out where is referencing this object?
>>
>>
>> Likely, it is the reference, you are holding:
>
>
> Unless you've just Py_DECREFed it, expecting it to go
> away, and the recfcount is still 1, in which case there's
> still another reference somewhere else.

If you've Py_DECREFed it and then peek into its internals, you're
aiming a gun at your foot. In theory, the decref could have been
followed by some other operation (or a context switch) that allocated
another object and happened to use the same address (not as unlikely
as you might think, given that some object types use free lists).
Always have a reference to something before you mess with it.

ChrisA



More information about the Python-list mailing list