Skip to content

Commit

Permalink
python: Free XML string after use
Browse files Browse the repository at this point in the history
Now that iio_context_get_xml() returns a heap-allocated string, we need
to de-allocate it with free() after use.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
  • Loading branch information
pcercuei committed Jan 30, 2024
1 parent 58127c8 commit 8c6922f
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions bindings/python/iio.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
c_double,
cast,
sizeof,
string_at,
POINTER as _POINTER,
CDLL as _cdll,
memmove as _memmove,
Expand Down Expand Up @@ -300,6 +301,7 @@ class ChannelType(Enum):
_iiolib = "iio"

_lib = _cdll("libiio.so.1", use_errno=True, use_last_error=True)
_libc = _cdll(find_library("c"))

_get_backends_count = _lib.iio_get_builtin_backends_count
_get_backends_count.restype = c_uint
Expand Down Expand Up @@ -357,7 +359,7 @@ class ChannelType(Enum):
_get_description.argtypes = (_ContextPtr,)

_get_xml = _lib.iio_context_get_xml
_get_xml.restype = c_char_p
_get_xml.restype = c_void_p
_get_xml.argtypes = (_ContextPtr,)

_get_version_major = _lib.iio_context_get_version_major
Expand Down Expand Up @@ -697,6 +699,9 @@ class ChannelType(Enum):
_ev_get_channel.argtypes = (_EventPtr, _DevicePtr, c_bool)
_ev_get_channel.errcheck = _check_null

_libc_free = _libc.free
_libc_free.argtypes = (c_void_p,)

# pylint: enable=invalid-name


Expand Down Expand Up @@ -1419,13 +1424,15 @@ def __init__(self, _context=None):

self._name = _get_name(self._context).decode("ascii")
self._description = _get_description(self._context).decode("ascii")
self._xml = _get_xml(self._context).decode("ascii")
self._xml_hdl = _get_xml(self._context)
self._xml = bytes(string_at(self._xml_hdl)).decode("ascii")
self._version = _get_lib_version(self._context)

def __del__(self):
"""Destroy this context."""
if self._context is not None:
_destroy(self._context)
_libc_free(cast(self._xml_hdl, c_void_p))

def set_timeout(self, timeout):
"""
Expand Down

0 comments on commit 8c6922f

Please sign in to comment.