This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: reference leak in _PyUnicode_AsDefaultEncodedString
Type: Stage:
Components: Interpreter Core Versions: Python 3.0
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: gvanrossum Nosy List: amaury.forgeotdarc, gvanrossum
Priority: normal Keywords: patch

Created on 2007-07-11 23:57 by amaury.forgeotdarc, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg52859 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2007-07-11 23:57
If I understand correctlty, _PyUnicode_AsDefaultEncodedString creates and caches a utf8 translation of the string. It returns a borrowed reference to this.
But the stored value (in ->defenc) has a refcount of 2, and will never be released when the unicode object is freed.

The effect is obvious in debug build: the total references count used to increase by 1 for each function call.

The patch is easy:

Index: Objects/unicodeobject.c
=======================================================
--- Objects/unicodeobject.c     (revision 56284)
+++ Objects/unicodeobject.c     (working copy)
@@ -1207,7 +1213,6 @@
                                    PyBytes_Size(b));
     Py_DECREF(b);
     if (!errors) {
-        Py_XINCREF(v);
         ((PyUnicodeObject *)unicode)->defenc = v;
     }
     return v;

msg52860 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2007-07-12 00:00
Of course, sourceforge ate the spaces again... sorry.
Here is the patch again:

Index: Objects/unicodeobject.c
===================================================================
--- Objects/unicodeobject.c     (revision 56284)
+++ Objects/unicodeobject.c     (working copy)
@@ -1207,7 +1207,6 @@
                                    PyBytes_Size(b));
     Py_DECREF(b);
     if (!errors) {
-        Py_XINCREF(v);
         ((PyUnicodeObject *)unicode)->defenc = v;
     }
     return v;
msg52861 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-07-12 07:53
Good analysis!  Fixed.

Committed revision 56288.
History
Date User Action Args
2022-04-11 14:56:25adminsetgithub: 45182
2008-01-06 22:29:45adminsetkeywords: - py3k
versions: + Python 3.0
2007-07-11 23:57:32amaury.forgeotdarccreate