* [python] Fix two memory leaks
@ 2009-02-25 18:29 Phil Muldoon
2009-02-25 18:39 ` Tom Tromey
0 siblings, 1 reply; 3+ messages in thread
From: Phil Muldoon @ 2009-02-25 18:29 UTC (permalink / raw)
To: Project Archer
[-- Attachment #1: Type: text/plain, Size: 1344 bytes --]
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.
[-- Attachment #2: unicode_decref.patch --]
[-- Type: text/x-patch, Size: 1401 bytes --]
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
[-- Attachment #3: value_decr.patch --]
[-- Type: text/x-patch, Size: 426 bytes --]
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;
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [python] Fix two memory leaks
2009-02-25 18:29 [python] Fix two memory leaks Phil Muldoon
@ 2009-02-25 18:39 ` Tom Tromey
2009-02-26 8:50 ` Phil Muldoon
0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2009-02-25 18:39 UTC (permalink / raw)
To: Phil Muldoon; +Cc: Project Archer
>>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes:
Phil> The unicode_decref patch changes the:
Phil> "python-utils.c/python_string_to_unicode" function to always return a
Phil> new reference.
Could you submit this to gdb-patches? This utility code is already
there, so the fix needs to be as well. Thanks.
Phil> The second patch is a straightforward addition of a variable
Phil> dereference inside the function:
Phil> "python.c/apply_val_pretty_printer". This value object was never being
Phil> dereferenced.
This is ok, thanks.
Tom
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [python] Fix two memory leaks
2009-02-25 18:39 ` Tom Tromey
@ 2009-02-26 8:50 ` Phil Muldoon
0 siblings, 0 replies; 3+ messages in thread
From: Phil Muldoon @ 2009-02-26 8:50 UTC (permalink / raw)
To: Tom Tromey; +Cc: Project Archer
Tom Tromey wrote:
>>>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes:
>>>>>>
>
> Phil> The unicode_decref patch changes the:
> Phil> "python-utils.c/python_string_to_unicode" function to always return a
> Phil> new reference.
>
> Could you submit this to gdb-patches? This utility code is already
> there, so the fix needs to be as well. Thanks.
>
Done. I also committed this to the archer-tromey-python branch. The
commit is:
b5235efef9aa1d619d8a5defc5aa1219dff76d3d
We should revert it should we merge master -> archer-tromey-python after
this particular fix has been accepted and committed upstream.
> Phil> The second patch is a straightforward addition of a variable
> Phil> dereference inside the function:
> Phil> "python.c/apply_val_pretty_printer". This value object was never being
> Phil> dereferenced.
>
> This is ok, thanks
Committed.
Regards
Phil
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2009-02-26 8:50 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-02-25 18:29 [python] Fix two memory leaks Phil Muldoon
2009-02-25 18:39 ` Tom Tromey
2009-02-26 8:50 ` Phil Muldoon
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).