Skip to content

Commit

Permalink
c++: 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 8c6922f commit 37f3bf8
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion bindings/cpp/iiopp.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class cstr
public:
cstr(std::string const & s) : s(s.c_str()){}
cstr(char const * s) : s(s){assert(s);}
cstr(void const * s) : s(static_cast<char const *>(s)){assert(s);}

char const * c_str() const {return s;}
operator char const * () const {return s;}
Expand Down Expand Up @@ -580,6 +581,8 @@ class Device : public impl::IndexedSequence<Device, Channel>
uint32_t reg_read(uint32_t address) {uint32_t value; impl::check(iio_device_reg_read(p, address, &value), "iio_device_reg_read"); return value;}
};

typedef Ptr<cstr, void, free> CstrPtr;

/** @brief C++ wrapper for the @ref Context C-API
*/
class Context : public impl::IndexedSequence<Context, Device>
Expand Down Expand Up @@ -617,7 +620,7 @@ class Context : public impl::IndexedSequence<Context, Device>
unsigned int version_major() const { return iio_context_get_version_major(p); }
unsigned int version_minor() const { return iio_context_get_version_minor(p); }
cstr version_tag() const { return iio_context_get_version_tag(p); }
cstr xml() const { return iio_context_get_xml(p); }
CstrPtr xml() const { return CstrPtr{impl::check(iio_context_get_xml(p), "iio_context_get_xml")};}
cstr name() const { return iio_context_get_name(p); }
cstr description() const { return iio_context_get_description(p); }
unsigned int attrs_count() const {return iio_context_get_attrs_count(p);}
Expand Down

0 comments on commit 37f3bf8

Please sign in to comment.