[Python-Dev] Small memory leak in unicodeobject.c

M.-A. Lemburg mal@lemburg.com
Tue, 03 Oct 2000 19:13:40 +0200


"Barry A. Warsaw" wrote:
> 
> 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! ;} ).

It would be easier to simply move the Py_DECREF() in front of the
cleanup code for the free list. That way, unicode_empty would
be placed on the free list, but then immedialtely cleaned up
by the following code.

Should I check in such a patch ?
 
> 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;
>  }
> 
> _______________________________________________
> Python-Dev mailing list
> Python-Dev@python.org
> http://www.python.org/mailman/listinfo/python-dev

-- 
Marc-Andre Lemburg
______________________________________________________________________
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/