[RFA] Fix a bunch of python leaks due to missing calls to tp_free in *_dealloc functions.

Message ID 20191117220919.25298-1-philippe.waroquiers@skynet.be
State New
Headers show
Series
  • [RFA] Fix a bunch of python leaks due to missing calls to tp_free in *_dealloc functions.
Related show

Commit Message

Philippe Waroquiers Nov. 17, 2019, 10:09 p.m.
valgrind reports leaks in many python tests, such as:
==17162== VALGRIND_GDB_ERROR_BEGIN
==17162== 8,208 (5,472 direct, 2,736 indirect) bytes in 57 blocks are definitely lost in loss record 7,551 of 7,679
==17162==    at 0x4835753: malloc (vg_replace_malloc.c:307)
==17162==    by 0x6EAFD1: _PyObject_New (object.c:279)
==17162==    by 0x4720E6: blpy_iter(_object*) (py-block.c:92)
==17162==    by 0x698772: PyObject_GetIter (abstract.c:2577)
==17162==    by 0x2343BE: _PyEval_EvalFrameDefault (ceval.c:3159)
==17162==    by 0x22E9E2: function_code_fastcall (call.c:283)
==17162==    by 0x2340A8: _PyObject_Vectorcall (abstract.h:127)
==17162==    by 0x2340A8: call_function (ceval.c:4987)
==17162==    by 0x2340A8: _PyEval_EvalFrameDefault (ceval.c:3486)
==17162==    by 0x22E9E2: function_code_fastcall (call.c:283)
==17162==    by 0x82172B: _PyObject_Vectorcall (abstract.h:127)
==17162==    by 0x82172B: method_vectorcall (classobject.c:67)
==17162==    by 0x6AF474: _PyObject_Vectorcall (abstract.h:127)
==17162==    by 0x6AF474: _PyObject_CallNoArg (abstract.h:153)
==17162==    by 0x6AF474: _PyObject_CallFunctionVa (call.c:914)
==17162==    by 0x6B0673: callmethod (call.c:1010)
==17162==    by 0x6B0673: _PyObject_CallMethod_SizeT (call.c:1103)
==17162==    by 0x477DFE: gdb_PyObject_CallMethod<> (python-internal.h:182)
==17162==    by 0x477DFE: get_py_iter_from_func(_object*, char const*) (py-framefilter.c:272)
==17162==    by 0x4791B4: py_print_args (py-framefilter.c:706)
==17162==    by 0x4791B4: py_print_frame(_object*, enum_flags<frame_filter_flag>, ext_lang_frame_args, ui_out*, int, htab*) (py-framefilter.c:960)
==17162==    by 0x47A130: gdbpy_apply_frame_filter(extension_language_defn const*, frame_info*, enum_flags<frame_filter_flag>, ext_lang_frame_args, ui_out*, int, int) (py-framefilter.c:1236)
==17162==    by 0x369C39: apply_ext_lang_frame_filter(frame_info*, enum_flags<frame_filter_flag>, ext_lang_frame_args, ui_out*, int, int) (extension.c:563)
==17162==    by 0x4EC9C9: backtrace_command_1 (stack.c:2031)
==17162==    by 0x4EC9C9: backtrace_command(char const*, int) (stack.c:2183)
...

Most of the leaks in python tests are due to the fact that many
PyObject xxxxx_dealloc functions are missing the line to free self
or obj such as:
   Py_TYPE (self)->tp_free (self);
or
   Py_TYPE (obj)->tp_free (obj);

With this patch, the number of python tests leaking decreases from 52 to 12.

gdb/ChangeLog

YYYY-MM-DD  Philippe Waroquiers  <philippe.waroquiers@skynet.be>

	* python/py-block.c (blpy_dealloc): Call tp_free.
	(blpy_block_syms_dealloc): Likewise.
	* python/py-finishbreakpoint.c (bpfinishpy_dealloc): Likewise.
	* python/py-inferior.c (infpy_dealloc): Likewise.
	* python/py-lazy-string.c (stpy_dealloc): Likewise.
	* python/py-linetable.c (ltpy_iterator_dealloc): Likewise.
	* python/py-symbol.c (sympy_dealloc): Likewise.
	* python/py-symtab.c (stpy_dealloc): Likewise.
	* python/py-type.c (typy_iterator_dealloc): Likewise.
---
 gdb/python/py-block.c            | 2 ++
 gdb/python/py-finishbreakpoint.c | 1 +
 gdb/python/py-inferior.c         | 1 +
 gdb/python/py-lazy-string.c      | 1 +
 gdb/python/py-linetable.c        | 1 +
 gdb/python/py-symbol.c           | 1 +
 gdb/python/py-symtab.c           | 1 +
 gdb/python/py-type.c             | 1 +
 8 files changed, 9 insertions(+)

-- 
2.20.1

Comments

Tom Tromey Nov. 18, 2019, 7:01 p.m. | #1
>>>>> "Philippe" == Philippe Waroquiers <philippe.waroquiers@skynet.be> writes:


Philippe> Most of the leaks in python tests are due to the fact that many
Philippe> PyObject xxxxx_dealloc functions are missing the line to free self
Philippe> or obj such as:
Philippe>    Py_TYPE (self)->tp_free (self);
Philippe> or
Philippe>    Py_TYPE (obj)->tp_free (obj);

Philippe> With this patch, the number of python tests leaking decreases from 52 to 12.

Thank you.  This is ok.

Tom

Patch

diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c
index 4dc47ff169..54c3be9b1a 100644
--- a/gdb/python/py-block.c
+++ b/gdb/python/py-block.c
@@ -276,6 +276,7 @@  blpy_dealloc (PyObject *obj)
   if (block->next)
     block->next->prev = block->prev;
   block->block = NULL;
+  Py_TYPE (obj)->tp_free (obj);
 }
 
 /* Given a block, and a block_object that has previously been
@@ -370,6 +371,7 @@  blpy_block_syms_dealloc (PyObject *obj)
   block_syms_iterator_object *iter_obj = (block_syms_iterator_object *) obj;
 
   Py_XDECREF (iter_obj->source);
+  Py_TYPE (obj)->tp_free (obj);
 }
 
 /* Implementation of gdb.Block.is_valid (self) -> Boolean.
diff --git a/gdb/python/py-finishbreakpoint.c b/gdb/python/py-finishbreakpoint.c
index 7f818213bd..fc20575aea 100644
--- a/gdb/python/py-finishbreakpoint.c
+++ b/gdb/python/py-finishbreakpoint.c
@@ -83,6 +83,7 @@  bpfinishpy_dealloc (PyObject *self)
   Py_XDECREF (self_bpfinish->function_value);
   Py_XDECREF (self_bpfinish->return_type);
   Py_XDECREF (self_bpfinish->return_value);
+  Py_TYPE (self)->tp_free (self);
 }
 
 /* Triggered when gdbpy_should_stop is about to execute the `stop' callback
diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index 28882221a9..d475263df9 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -856,6 +856,7 @@  infpy_dealloc (PyObject *obj)
     return;
 
   set_inferior_data (inf, infpy_inf_data_key, NULL);
+  Py_TYPE (obj)->tp_free (obj);
 }
 
 /* Clear the INFERIOR pointer in an Inferior object and clear the
diff --git a/gdb/python/py-lazy-string.c b/gdb/python/py-lazy-string.c
index 87c6ceaf2a..74f14a94eb 100644
--- a/gdb/python/py-lazy-string.c
+++ b/gdb/python/py-lazy-string.c
@@ -156,6 +156,7 @@  stpy_dealloc (PyObject *self)
   lazy_string_object *self_string = (lazy_string_object *) self;
 
   xfree (self_string->encoding);
+  Py_TYPE (self)->tp_free (self);
 }
 
 /* Low level routine to create a <gdb.LazyString> object.
diff --git a/gdb/python/py-linetable.c b/gdb/python/py-linetable.c
index aa43d9e7c7..96a5d3515b 100644
--- a/gdb/python/py-linetable.c
+++ b/gdb/python/py-linetable.c
@@ -369,6 +369,7 @@  ltpy_iterator_dealloc (PyObject *obj)
   ltpy_iterator_object *iter_obj = (ltpy_iterator_object *) obj;
 
   Py_DECREF (iter_obj->source);
+  Py_TYPE (obj)->tp_free (obj);
 }
 
 /* Return a reference to the line table iterator.  */
diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index 647c54b0a5..b112ce115f 100644
--- a/gdb/python/py-symbol.c
+++ b/gdb/python/py-symbol.c
@@ -357,6 +357,7 @@  sympy_dealloc (PyObject *obj)
   if (sym_obj->next)
     sym_obj->next->prev = sym_obj->prev;
   sym_obj->symbol = NULL;
+  Py_TYPE (obj)->tp_free (obj);
 }
 
 /* Implementation of
diff --git a/gdb/python/py-symtab.c b/gdb/python/py-symtab.c
index 15af300150..49c6a0ac54 100644
--- a/gdb/python/py-symtab.c
+++ b/gdb/python/py-symtab.c
@@ -253,6 +253,7 @@  stpy_dealloc (PyObject *obj)
   if (symtab->next)
     symtab->next->prev = symtab->prev;
   symtab->symtab = NULL;
+  Py_TYPE (obj)->tp_free (obj);
 }
 
 
diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index a2fca97a32..e4350993dd 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -1325,6 +1325,7 @@  typy_iterator_dealloc (PyObject *obj)
   typy_iterator_object *iter_obj = (typy_iterator_object *) obj;
 
   Py_DECREF (iter_obj->source);
+  Py_TYPE (obj)->tp_free (obj);
 }
 
 /* Create a new Type referring to TYPE.  */