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<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

2019-11-18  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.
This commit is contained in:
Philippe Waroquiers 2019-11-17 22:48:48 +01:00
parent 6edc43ec32
commit 2e953acac2
9 changed files with 21 additions and 0 deletions

View File

@ -1,3 +1,15 @@
2019-11-18 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.
2019-11-18 Christian Biesinger <cbiesinger@google.com>
* symtab.h (struct symbol) <owner>: Initialize explicitly in the

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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.

View File

@ -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. */

View File

@ -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

View File

@ -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);
}

View File

@ -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. */