From 29adcc24da769d31a657fac747faebdca4f16a05 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Tue, 24 Jul 2018 14:12:18 +0200 Subject: [PATCH] bindings: python: fix tp_dealloc callbacks in all objects MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit We have a significant memory leak in the current implementation as we don't call PyObject_Del() as the last action in tp_dealloc callbacks. From tp_dealloc's documentation: --- The destructor function should free all references which the instance owns, free all memory buffers owned by the instance (using the freeing function corresponding to the allocation function used to allocate the buffer), and finally (as its last action) call the type’s tp_free function. --- PyObject_Del() will internally call the tp_free callback of the type. Add a call to it as the last action in every destructor. Fixes: 96c524c4951c ("bindings: implement python bindings") Signed-off-by: Bartosz Golaszewski --- bindings/python/gpiodmodule.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bindings/python/gpiodmodule.c b/bindings/python/gpiodmodule.c index f90ccd5..8e576a9 100644 --- a/bindings/python/gpiodmodule.c +++ b/bindings/python/gpiodmodule.c @@ -114,6 +114,8 @@ static void gpiod_LineEvent_dealloc(gpiod_LineEventObject *self) { if (self->source) Py_DECREF(self->source); + + PyObject_Del(self); } PyDoc_STRVAR(gpiod_LineEvent_get_type_doc, @@ -228,6 +230,8 @@ static void gpiod_Line_dealloc(gpiod_LineObject *self) { if (self->owner) Py_DECREF(self->owner); + + PyObject_Del(self); } PyDoc_STRVAR(gpiod_Line_owner_doc, @@ -783,6 +787,7 @@ static void gpiod_LineBulk_dealloc(gpiod_LineBulkObject *self) Py_DECREF(self->lines[i]); PyMem_Free(self->lines); + PyObject_Del(self); } static PyObject *gpiod_LineBulk_iternext(gpiod_LineBulkObject *self) @@ -1296,6 +1301,8 @@ static void gpiod_Chip_dealloc(gpiod_ChipObject *self) { if (self->chip) gpiod_chip_close(self->chip); + + PyObject_Del(self); } static PyObject *gpiod_Chip_repr(gpiod_ChipObject *self) @@ -1763,6 +1770,8 @@ static void gpiod_ChipIter_dealloc(gpiod_ChipIterObject *self) { if (self->iter) gpiod_chip_iter_free_noclose(self->iter); + + PyObject_Del(self); } static gpiod_ChipObject *gpiod_ChipIter_next(gpiod_ChipIterObject *self) @@ -1834,6 +1843,8 @@ static void gpiod_LineIter_dealloc(gpiod_LineIterObject *self) { if (self->iter) gpiod_line_iter_free(self->iter); + + PyObject_Del(self); } static gpiod_LineObject *gpiod_LineIter_next(gpiod_LineIterObject *self) -- 2.30.2