This is the mail archive of the
archer@sourceware.org
mailing list for the Archer project.
[python] Fix two memory leaks
- From: Phil Muldoon <pmuldoon at redhat dot com>
- To: Project Archer <archer at sourceware dot org>
- Date: Wed, 25 Feb 2009 18:29:13 +0000
- Subject: [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:
http://sources.redhat.com/ml/archer/2009-q1/msg00240.html
And reviewed in this one:
http://sources.redhat.com/ml/archer/2009-q1/msg00248.html
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.
Regards
Phil
ChangeLog
2009-02-25 Phil Muldoon <pmuldoon@redhat.com>
* 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
instance.
(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);
else
@@ -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;