History

perf python: Fix count_values memory leak in pyrf_evsel__read

In pyrf_evsel__read, if PyArg_ParseTuple fails, the allocated count_values is leaked. Move the allocation of count_values after the PyArg_ParseTuple call to prevent the memory leak.

Ian Rogers committed 3 months ago

Commit cb45ede1 parent b9514a91 file · +4 −5Browse files

Showing the changes to tools/perf/util/python.c (1 of 1 file). Show all

Unified · Split

tools/perf/util/python.c+4−5
@@ -2211,11 +2211,6 @@ static PyObject *pyrf_evsel__read(struct pyrf_evsel *pevsel, · 2210 unmodified lines
22112211
22122212 CHECK_INITIALIZED(evsel, "evsel");
22132213
2214− count_values = PyObject_New(struct pyrf_counts_values,
2215− &pyrf_counts_values__type);
2216− if (!count_values)
2217− return NULL;
2218−
22192214 if (!PyArg_ParseTuple(args, "ii", &cpu, &thread))
22202215 return NULL;
22212216
@@ -2234,6 +2229,10 @@ static PyObject *pyrf_evsel__read(struct pyrf_evsel *pevsel, · 12 unmodified lines
22342229 if (evsel__ensure_counts(evsel))
22352230 return PyErr_NoMemory();
22362231
2232+ count_values = PyObject_New(struct pyrf_counts_values, &pyrf_counts_values__type);
2233+ if (!count_values)
2234+ return NULL;
2235+
22372236 /* Set up pointers to the old and newly read counter values. */
22382237 old_count = perf_counts(evsel->prev_raw_counts, cpu_idx, thread_idx);
22392238 new_count = perf_counts(evsel->counts, cpu_idx, thread_idx);