[Python-Dev] Small memory leak in unicodeobject.c
Barry A. Warsaw
bwarsaw@beopen.com
Tue, 3 Oct 2000 12:34:51 -0400 (EDT)
I've found a small memory leak in unicodeobject.c, in the way
_PyUnicode_Fini() shuts down. Take a loop such as:
-------------------- snip snip --------------------
#include "Python.h"
int main(int argc, char** argv)
{
int i;
while (1) {
Py_Initialize();
Py_Finalize();
}
return 0;
}
-------------------- snip snip --------------------
and you'll find that unicode_empty leaks. This is because, while
_PyUnicode_Fini() decrefs unicode_empty, it never actually gets
freed. Instead it gets placed on the already freed unicode_freelist!
See _PyUnicode_Free() for why.
I've come up with the following nasty hack to force unicode_empty to
really be freed in _PyUnicode_Fini(). Maybe there's a better way to
do this. Note that moving the decref of unicode_empty to above the
freeing of unicode_freelist didn't plug the memory leak for me (but
I'm quite tired so maybe I missed something! ;} ).
Below is the proposed patch.
-Barry
-------------------- snip snip --------------------
Index: unicodeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.64
diff -u -r2.64 unicodeobject.c
--- unicodeobject.c 2000/09/26 05:46:01 2.64
+++ unicodeobject.c 2000/10/03 16:31:32
@@ -5234,7 +5234,11 @@
PyObject_DEL(v);
}
unicode_freelist = NULL;
- unicode_freelist_size = 0;
+ /* XXX This is a hack to force the freeing of unicode_empty's memory.
+ * Otherwise, it'll get placed on the already freed free list.
+ */
+ unicode_freelist_size = MAX_UNICODE_FREELIST_SIZE;
Py_XDECREF(unicode_empty);
unicode_empty = NULL;
+ unicode_freelist_size = 0;
}