From 2e953acac2ef45afa10e0b2c4c6c23fb52718a26 Mon Sep 17 00:00:00 2001 From: Philippe Waroquiers Date: Sun, 17 Nov 2019 22:48:48 +0100 Subject: Fix a bunch of python leaks due to missing calls to tp_free in *_dealloc functions. 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, 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, ext_lang_frame_args, ui_out*, int, int) (py-framefilter.c:1236) ==17162== by 0x369C39: apply_ext_lang_frame_filter(frame_info*, enum_flags, 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 2019-11-18 Philippe Waroquiers * 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/ChangeLog | 12 ++++++++++++ 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 + 9 files changed, 21 insertions(+) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 0e17d7c39d8..0dfc96ade11 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,15 @@ +2019-11-18 Philippe Waroquiers + + * 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. + 2019-11-18 Christian Biesinger * symtab.h (struct symbol) : Initialize explicitly in the diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c index 4dc47ff169d..54c3be9b1a4 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 7f818213bd2..fc20575aea6 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 28882221a90..d475263df99 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 87c6ceaf2a4..74f14a94eb9 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 object. diff --git a/gdb/python/py-linetable.c b/gdb/python/py-linetable.c index aa43d9e7c74..96a5d3515b6 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 647c54b0a5b..b112ce115f1 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 15af3001505..49c6a0ac544 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 a2fca97a327..e4350993dd4 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. */ -- cgit v1.2.3-65-gdbad