From: Bartosz Golaszewski Date: Sat, 12 May 2018 12:30:44 +0000 (+0200) Subject: bindings: python: decref the chip object in gpiod_Module_find_line() X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=f36a27f1cc25167c1f03fea8f6f431e10375c043;p=qemu-gpiodev%2Flibgpiod.git bindings: python: decref the chip object in gpiod_Module_find_line() When returning back to the interpreter from gpiod_Module_find_line(), there's only one chip object reference holder, but we set the refcount to 2 (as if the chip object existed separately in the interpreter). Fix it by decreasing the chip's refcount by one before returning the line object. Signed-off-by: Bartosz Golaszewski --- diff --git a/bindings/python/gpiodmodule.c b/bindings/python/gpiodmodule.c index 2b4eddc..8e36ec8 100644 --- a/bindings/python/gpiodmodule.c +++ b/bindings/python/gpiodmodule.c @@ -1558,6 +1558,7 @@ PyDoc_STRVAR(gpiod_Module_find_line_doc, static gpiod_LineObject *gpiod_Module_find_line(PyObject *self GPIOD_UNUSED, PyObject *args) { + gpiod_LineObject *line_obj; gpiod_ChipObject *chip_obj; struct gpiod_chip *chip; struct gpiod_line *line; @@ -1586,7 +1587,20 @@ static gpiod_LineObject *gpiod_Module_find_line(PyObject *self GPIOD_UNUSED, chip_obj->chip = chip; - return gpiod_MakeLineObject(chip_obj, line); + line_obj = gpiod_MakeLineObject(chip_obj, line); + if (!line_obj) + return NULL; + + /* + * PyObject_New() set the reference count for the chip object at 1 and + * the call to gpiod_MakeLineObject() increased it to 2. However when + * we return the object to the line object to the python interpreter, + * there'll be only a single reference holder to the chip - the line + * object itself. Decrease the chip reference here manually. + */ + Py_DECREF(line_obj->owner); + + return line_obj; } static PyMethodDef gpiod_module_methods[] = {