[PATCH] parsers: fix memory leak in compute_phases_map_sets

Laurent Charignon lcharignon at fb.com
Fri Aug 7 06:01:07 UTC 2015


# HG changeset patch
# User Laurent Charignon <lcharignon at fb.com>
# Date 1438926868 25200
#      Thu Aug 06 22:54:28 2015 -0700
# Branch stable
# Node ID 219562bcc696de725990f9f103b380aefa1abd66
# Parent  79f0cb97d7537a7c2948f8f9b0a89148825a3a1d
parsers: fix memory leak in compute_phases_map_sets

PySet_Add increments the reference of the added object to the set, see:
https://github.com/python/cpython/blob/2.6/Objects/setobject.c
Before this patch we were forgetting to decrement the reference count after
adding objects to the phaseset. This patch fixes the issue and makes the
reference count right so that these objects can be properly garbage collected.

diff --git a/mercurial/parsers.c b/mercurial/parsers.c
--- a/mercurial/parsers.c
+++ b/mercurial/parsers.c
@@ -1113,6 +1113,7 @@
 	PyObject *phaseroots = NULL;
 	PyObject *phaseset = NULL;
 	PyObject *phasessetlist = NULL;
+	PyObject *rev = NULL;
 	Py_ssize_t len = index_length(self) - 1;
 	Py_ssize_t numphase = 0;
 	Py_ssize_t minrevallphases = 0;
@@ -1172,7 +1173,9 @@
 		 * is computed as a difference */
 		if (phase != 0) {
 			phaseset = PyList_GET_ITEM(phasessetlist, phase);
-			PySet_Add(phaseset, PyInt_FromLong(i));
+			rev = PyInt_FromLong(i);
+			PySet_Add(phaseset, rev);
+			Py_XDECREF(rev);
 		}
 		PyList_SET_ITEM(phaseslist, i, PyInt_FromLong(phase));
 	}


More information about the Mercurial-devel mailing list