bindings: python: decref the chip object in gpiod_Module_find_line()
authorBartosz Golaszewski <bartekgola@gmail.com>
Sat, 12 May 2018 12:30:44 +0000 (14:30 +0200)
committerBartosz Golaszewski <bartekgola@gmail.com>
Sat, 12 May 2018 12:51:28 +0000 (14:51 +0200)
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 <bartekgola@gmail.com>
bindings/python/gpiodmodule.c

index 2b4eddc9030265689b07d46cea91885a49fd7b27..8e36ec82bef3a191a722f95fe1a7b3987b146973 100644 (file)
@@ -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[] = {