[Python-checkins] CVS: python/dist/src/Objects unicodeobject.c,2.123,2.124

Guido van Rossum gvanrossum@users.sourceforge.net
Thu, 06 Dec 2001 12:03:59 -0800


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv11937

Modified Files:
	unicodeobject.c 
Log Message:
Fix for #489669 (Neil Norwitz): memory leak in test_descr (unicode).

This is best reproduced by

  while 1:
      class U(unicode):
          pass
      U(u"xxxxxx")

The unicode_dealloc() code wasn't properly freeing the str and defenc
fields of the Unicode object when freeing a subtype instance.  Fixed
this by a subtle refactoring that actually reduces the amount of code
slightly.


Index: unicodeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.123
retrieving revision 2.124
diff -C2 -d -r2.123 -r2.124
*** unicodeobject.c	2001/11/28 21:00:41	2.123
--- unicodeobject.c	2001/12/06 20:03:56	2.124
***************
*** 227,235 ****
  void unicode_dealloc(register PyUnicodeObject *unicode)
  {
!     if (!PyUnicode_CheckExact(unicode)) {
! 	unicode->ob_type->tp_free((PyObject *)unicode);
! 	return;
!     }
!     if (unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) {
          /* Keep-Alive optimization */
  	if (unicode->length >= KEEPALIVE_SIZE_LIMIT) {
--- 227,232 ----
  void unicode_dealloc(register PyUnicodeObject *unicode)
  {
!     if (PyUnicode_CheckExact(unicode) &&
! 	unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) {
          /* Keep-Alive optimization */
  	if (unicode->length >= KEEPALIVE_SIZE_LIMIT) {
***************
*** 250,254 ****
  	PyMem_DEL(unicode->str);
  	Py_XDECREF(unicode->defenc);
! 	PyObject_DEL(unicode);
      }
  }
--- 247,251 ----
  	PyMem_DEL(unicode->str);
  	Py_XDECREF(unicode->defenc);
! 	unicode->ob_type->tp_free((PyObject *)unicode);
      }
  }