[Python] Patch to fix memory leak

Paul Koning paulkoning@comcast.net
Wed Sep 7 12:29:00 GMT 2011


On Sep 7, 2011, at 5:27 AM, Phil Muldoon wrote:

> Paul Koning <paulkoning@comcast.net> writes:
> 
>> PyList_Append increments the reference count of the item being
>> appended to the list, and two of the places in the GDB Python code
>> that use it don't take that into account.  The result is a memory
>> leak.  The attached patch fixes that.
> 
> For some reason the Python API manual is silent about PyList_Append
> incrementing the reference count.  But I did check, and you are correct,
> the reference count is incremented when added to a list.  

Apart from looking at the source, I find the most helpful statement is in the C API document, Introduction, Reference Count details, which says that when you pass an object as an argument the called function may "steal" the reference, or not -- and few functions steal the reference.  (For that matter, few functions return borrowed references.)  So the general rule is that passed objects are not stolen, and returned objects are new references.  Stolen and borrowed references are mentioned explicitly in the documentation of the function in question, and silence means not borrowed, not stolen.

>> Tested by using a debug build of Python with reference counting
>> enabled, and turning on dumping of final leftover objects.
> 
> This is fine, thanks for doing this.  I cannot give you check-in
> permission as I am not a maintainer.  Please wait until one of the
> maintainers signs-off before you go ahead.

I don't have write privs so I'm relying on someone else to do the commit once approved.

	paul



More information about the Gdb-patches mailing list