Skip to content

Commit

Permalink
Merge pull request #93 from Gadgetoid/patch-rxbuf-memory-leak
Browse files Browse the repository at this point in the history
Call Py_DECREF to fix leaked reference counts, should fix #91
  • Loading branch information
doceme authored May 28, 2020
2 parents f543ca1 + 05deb4d commit 3239755
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions spidev_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ SpiDev_readbytes(SpiDevObject *self, PyObject *args)
list = PyList_New(len);

for (ii = 0; ii < len; ii++) {
PyObject *val = Py_BuildValue("l", (long)rxbuf[ii]);
PyList_SET_ITEM(list, ii, val);
PyObject *val = PyLong_FromLong((long)rxbuf[ii]);
PyList_SET_ITEM(list, ii, val); // Steals reference, no need to Py_DECREF(val)
}

return list;
Expand Down Expand Up @@ -586,8 +586,9 @@ SpiDev_xfer(SpiDevObject *self, PyObject *args)
#endif

for (ii = 0; ii < len; ii++) {
PyObject *val = Py_BuildValue("l", (long)rxbuf[ii]);
PyObject *val = PyLong_FromLong((long)rxbuf[ii]);
PySequence_SetItem(seq, ii, val);
Py_DECREF(val); // PySequence_SetItem does not steal reference, must Py_DECREF(val)
}

// WA:
Expand Down Expand Up @@ -696,8 +697,9 @@ SpiDev_xfer2(SpiDevObject *self, PyObject *args)
}

for (ii = 0; ii < len; ii++) {
PyObject *val = Py_BuildValue("l", (long)rxbuf[ii]);
PyObject *val = PyLong_FromLong((long)rxbuf[ii]);
PySequence_SetItem(seq, ii, val);
Py_DECREF(val); // PySequence_SetItem does not steal reference, must Py_DECREF(val)
}
// WA:
// in CS_HIGH mode CS isnt pulled to low after transfer
Expand Down Expand Up @@ -844,8 +846,8 @@ SpiDev_xfer3(SpiDevObject *self, PyObject *args)
return NULL;
}
for (ii = 0, jj = block_start; ii < block_size; ii++, jj++) {
PyObject *val = Py_BuildValue("l", (long)rxbuf[ii]);
PyTuple_SetItem(rx_tuple, jj, val);
PyObject *val = PyLong_FromLong((long)rxbuf[ii]);
PyTuple_SetItem(rx_tuple, jj, val); // Steals reference, no need to Py_DECREF(val)
}

block_start += block_size;
Expand Down

0 comments on commit 3239755

Please sign in to comment.