Fix for reference leak in current Python CVS

Charles G Waldman cgw at fnal.gov
Sat Mar 4 05:28:49 EST 2000


I noticed a reference leak in Python-1.5.2+ which is not present in
1.5.2.

You can witness the misbehaviour in the following session:

Python 1.5.2+ (#18, Mar  1 2000, 01:02:22)  [GCC 2.95.2 19991024
(release)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import sys, string
>>> a='spam'
>>> b='eggs'
>>> l=[a,b]
>>> sys.getrefcount(a)
5
>>> x=string.join(l,'')
>>> sys.getrefcount(a)
6
>>> x=string.join(l,'')
>>> sys.getrefcount(a)
7
>>> 

Apparently, somebody added code to `string_join' to do string coercion
on non-string sequence elements (a nice addition), but forgot to
decref the resulting values returned from PyObject_Str.

Here's a patch which adds the needed decref's.


Index: dist/src/Objects/stringobject.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.55
diff -u -r2.55 stringobject.c
--- stringobject.c	2000/02/29 13:59:28	2.55
+++ stringobject.c	2000/03/04 10:13:17
@@ -720,6 +720,7 @@
 				reslen += seplen;
 			}
 			memcpy(p, PyString_AS_STRING(sitem), slen);
+			Py_XDECREF(sitem);
 			p += slen;
 			reslen += slen;
 		}
@@ -745,6 +746,7 @@
 				reslen += seplen;
 			}
 			memcpy(p, PyString_AS_STRING(sitem), slen);
+			Py_XDECREF(sitem);
 			p += slen;
 			reslen += slen;
 		}




More information about the Python-list mailing list