From 3983d7b2308c27bf2eb40602fe22ae161d5e5e2d Mon Sep 17 00:00:00 2001 From: Yoann Vandoorselaere Date: Fri, 6 Feb 2015 14:30:47 +0100 Subject: [PATCH] Fix SwigPyObject->dict memory leak The following patch attempt to fix a memory leak happening when a random class attribute is set. The internal instance dictionary is created but never freed. This fixes the problem for me, although I am not sure the patch is correct. p = MySWIGClass() p.random_attribute = 0 Valgrind report: ==18267== 280 bytes in 1 blocks are definitely lost in loss record 1,372 of 1,780 ==18267== at 0x4A0645D: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) ==18267== by 0x3A90A885DC: PyObject_Malloc (in /usr/lib64/libpython2.7.so.1.0) ==18267== by 0x3A90B101A8: _PyObject_GC_Malloc (in /usr/lib64/libpython2.7.so.1.0) ==18267== by 0x3A90B102AC: _PyObject_GC_New (in /usr/lib64/libpython2.7.so.1.0) ==18267== by 0x3A90A80943: PyDict_New (in /usr/lib64/libpython2.7.so.1.0) ==18267== by 0x3A90A6E8FC: PyFrame_New (in /usr/lib64/libpython2.7.so.1.0) ==18267== by 0x3A90AE1A65: PyEval_EvalCodeEx (in /usr/lib64/libpython2.7.so.1.0) ==18267== by 0x3A90AE088E: PyEval_EvalFrameEx (in /usr/lib64/libpython2.7.so.1.0) ==18267== by 0x3A90AE21DC: PyEval_EvalCodeEx (in /usr/lib64/libpython2.7.so.1.0) ==18267== by 0x3A90AE22E1: PyEval_EvalCode (in /usr/lib64/libpython2.7.so.1.0) ==18267== by 0x3A90AFB71E: ??? (in /usr/lib64/libpython2.7.so.1.0) ==18267== by 0x3A90AFC8DD: PyRun_FileExFlags (in /usr/lib64/libpython2.7.so.1.0) ==18267== --- Lib/python/builtin.swg | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/python/builtin.swg b/Lib/python/builtin.swg index 28c557a21..1d892375c 100644 --- a/Lib/python/builtin.swg +++ b/Lib/python/builtin.swg @@ -9,6 +9,7 @@ SWIGINTERN void \ wrapper##_closure(PyObject *a) { \ SwigPyObject *sobj; \ sobj = (SwigPyObject *)a; \ + Py_XDECREF(sobj->dict); \ if (sobj->own) { \ PyObject *o = wrapper(a, NULL); \ Py_XDECREF(o); \