Perf: Fix possible memory leaks in Python interface The function PyObject_CallObject() returns a new PyObject reference on which Py_DECREF has to be called to avoid memory leaks. This patch adds these calls where necessary. Signed-off-by: Joseph Schuchart diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c index cd9774d..ee17f64 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c @@ -97,6 +97,8 @@ static void define_value(enum print_arg_type field_type, retval = PyObject_CallObject(handler, t); if (retval == NULL) handler_call_die(handler_name); + else + Py_DECREF(retval); } Py_DECREF(t); @@ -143,6 +145,8 @@ static void define_field(enum print_arg_type field_type, retval = PyObject_CallObject(handler, t); if (retval == NULL) handler_call_die(handler_name); + else + Py_DECREF(retval); } Py_DECREF(t); @@ -333,6 +337,8 @@ static void python_process_tracepoint(struct perf_sample *sample, retval = PyObject_CallObject(handler, t); if (retval == NULL) handler_call_die(handler_name); + else + Py_DECREF(retval); } else { handler = PyDict_GetItemString(main_dict, "trace_unhandled"); if (handler && PyCallable_Check(handler)) { @@ -340,6 +346,8 @@ static void python_process_tracepoint(struct perf_sample *sample, retval = PyObject_CallObject(handler, t); if (retval == NULL) handler_call_die("trace_unhandled"); + else + Py_DECREF(retval); } Py_DECREF(dict); } @@ -399,6 +407,8 @@ static void python_process_general_event(struct perf_sample *sample, retval = PyObject_CallObject(handler, t); if (retval == NULL) handler_call_die(handler_name); + else + Py_DECREF(retval); exit: Py_DECREF(dict); Py_DECREF(t); @@ -444,8 +454,8 @@ static int run_start_sub(void) retval = PyObject_CallObject(handler, NULL); if (retval == NULL) handler_call_die("trace_begin"); - - Py_DECREF(retval); + else + Py_DECREF(retval); return err; error: Py_XDECREF(main_dict);