This is the mail archive of the mailing list for the Archer project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[python] Fix two memory leaks

I'm submitting the first fix here for continuity, even though the unicode_decref.patch case was submitted in this message:

And reviewed in this one:

The unicode_decref patch changes the: "python-utils.c/python_string_to_unicode" function to always return a new reference. Previously it would return either a new or borrowed reference, which created ambiguity to the callers about clean-up. This resulted in a memory leak in some unicode string handling operations in GDB Python.

The second patch is a straightforward addition of a variable dereference inside the function: "python.c/apply_val_pretty_printer". This value object was never being dereferenced.

Hopefully I'll have the two other areas that I am investigating submitted soon. These fixes are not inter-dependent, so I decided to submit them as soon as possible.




2009-02-25 Phil Muldoon <>

   * python/python.c (apply_val_pretty_printer): Clean-up value
   instance after search.
   * python/python-utils.c (python_string_to_unicode): Always return
   a new reference.
   (python_string_to_target_string): Decrement transient python
   (python_string_to_host_string): Likewise.

diff --git a/gdb/python/python-utils.c b/gdb/python/python-utils.c
index c6c305f..f9c9486 100644
--- a/gdb/python/python-utils.c
+++ b/gdb/python/python-utils.c
@@ -82,7 +82,11 @@ python_string_to_unicode (PyObject *obj)
   /* If obj is already a unicode string, just return it.
      I wish life was always that simple...  */
   if (PyUnicode_Check (obj))
-    unicode_str = obj;
+    {
+      unicode_str = obj;
+      Py_INCREF (obj);
+    }
   else if (PyString_Check (obj))
     unicode_str = PyUnicode_FromEncodedObject (obj, host_charset (), NULL);
@@ -137,12 +141,15 @@ char *
 python_string_to_target_string (PyObject *obj)
   PyObject *str;
+  char *result;
   str = python_string_to_unicode (obj);
   if (str == NULL)
     return NULL;
-  return unicode_to_target_string (str);
+  result = unicode_to_target_string (str);
+  Py_DECREF (str);
+  return result;
 /* Converts a python string (8-bit or unicode) to a target string in
@@ -153,12 +160,15 @@ char *
 python_string_to_host_string (PyObject *obj)
   PyObject *str;
+  char *result;
   str = python_string_to_unicode (obj);
   if (str == NULL)
     return NULL;
-  return unicode_to_encoded_string (str, host_charset ());
+  result = unicode_to_encoded_string (str, host_charset ()); 
+  Py_DECREF (str);
+  return result;
 /* Converts a target string of LENGTH bytes in the target's charset to a
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 4d104c2..11b0aeb 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1527,6 +1527,7 @@ apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
   /* Find the constructor.  */
   printer = find_pretty_printer (val_obj);
+  Py_DECREF (val_obj);
   make_cleanup_py_decref (printer);
   if (! printer || printer == Py_None)
     goto done;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]