Puzzling behaviour of Py_IncRef

Tony Flury tony.flury at btinternet.com
Wed Jan 26 08:15:01 EST 2022


On 26/01/2022 08:20, Chris Angelico wrote:
> On Wed, 26 Jan 2022 at 19:04, Tony Flury via Python-list
> <python-list at python.org> wrote:
>> So according to that I should increment twice if and only if the calling
>> code is using the result - which you can't tell in the C code - which is
>> very odd behaviour.
> No, the return value from your C function will *always* have a
> reference taken. Whether the return value is "used" or just dropped,
> there's always going to be one ref used by the returning itself.
>
> The standard way to return a value is always to incref it, then return
> the pointer. That is exactly equivalent to Python saying "return
> <thing>".
>
> Incrementing twice is ONLY because you want to leak a reference.
>
> ChrisA

Chris,

You keep saying I am leaking a reference - my original code (not the POC 
in the email) wasn't intending to leak a reference, it was incrementing 
the reference count in order to accurately count references, from other 
objects and i needed to double increment there so that the reference 
count remained correct outside of the C code.

I did try to be clear - my intention was never to leak a reference (I 
have been writing s/w long enough to know leaks are bad) - my POC code 
in the original message was the only code which deliberately leaked a 
reference in order to simply illustrate the problem.

I do appreciate the help you have tried to give - so thank you.

-- 
Anthony Flury
email : anthony.flury at btinternet.com



More information about the Python-list mailing list