Skip to content

Commit

Permalink
python: Reference parent Device object from Buffer
Browse files Browse the repository at this point in the history
By having the Buffer objects reference their parent Device object,
without having the opposite, we can have a proper dependency graph, and
the objects will be freed in the correct order.

Signed-off-by: Paul Cercueil <[email protected]>
  • Loading branch information
pcercuei committed Aug 18, 2021
1 parent befeb37 commit 36240b4
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions bindings/python/iio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1018,9 +1018,10 @@ def __init__(self, device, samples_count, cyclic=False):
raise
self._length = samples_count * device.sample_size
self._samples_count = samples_count
self._ctx = device.ctx()
# Holds a reference to the corresponding IIO Context. This ensures that
# every iio.Buffer object is destroyed before its corresponding IIO Context.

# Hold a reference to the device, to ensure that every iio.Buffer object
# is destroyed before its corresponding IIO context.
self._dev = device

def __del__(self):
"""Destroy this buffer."""
Expand Down Expand Up @@ -1101,7 +1102,7 @@ def device(self):
Device for the buffer.
type: iio.Device
"""
return Device(self._ctx, _buffer_get_device(self._buffer))
return self._dev

@property
def poll_fd(self):
Expand Down

0 comments on commit 36240b4

Please sign in to comment.