From 4d331becdfef9c9cb7010e54333f1c9d53728e88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Soja?= Date: Fri, 23 Jun 2023 13:45:26 +0200 Subject: [PATCH 1/2] add setBlobDeleter function --- libs/indidevice/property/indipropertyblob.cpp | 17 ++++++++++++++++- libs/indidevice/property/indipropertyblob.h | 10 ++++++++++ libs/indidevice/property/indipropertyblob_p.h | 1 + 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/libs/indidevice/property/indipropertyblob.cpp b/libs/indidevice/property/indipropertyblob.cpp index 0c47010ad1..4cc0ff0298 100644 --- a/libs/indidevice/property/indipropertyblob.cpp +++ b/libs/indidevice/property/indipropertyblob.cpp @@ -27,7 +27,16 @@ PropertyBlobPrivate::PropertyBlobPrivate(size_t count) { } PropertyBlobPrivate::~PropertyBlobPrivate() -{ } +{ + for (auto &it: widgets) + { + auto blob = it.getBlob(); + if (blob != nullptr && deleter != nullptr) + { + deleter(blob); + } + } +} PropertyBlob::PropertyBlob(size_t count) : PropertyBasic(*new PropertyBlobPrivate(count)) @@ -40,6 +49,12 @@ PropertyBlob::PropertyBlob(INDI::Property property) PropertyBlob::~PropertyBlob() { } +void PropertyBlob::setBlobDeleter(const std::function &deleter) +{ + D_PTR(PropertyBlob); + d->deleter = deleter; +} + bool PropertyBlob::update( const int sizes[], const int blobsizes[], const char * const blobs[], const char * const formats[], const char * const names[], int n diff --git a/libs/indidevice/property/indipropertyblob.h b/libs/indidevice/property/indipropertyblob.h index af6f293644..936eb2b137 100644 --- a/libs/indidevice/property/indipropertyblob.h +++ b/libs/indidevice/property/indipropertyblob.h @@ -31,6 +31,16 @@ class PropertyBlob: public INDI::PropertyBasic PropertyBlob(size_t count); PropertyBlob(INDI::Property property); ~PropertyBlob(); + + public: + /** + * @brief Set the Blob Deleter function + * You can define a function to release the memory of the elements. + * The function will be executed when the PropertyBlob is destroyed + * + * @param deleter function to release the memory of a given item + */ + void setBlobDeleter(const std::function &deleter); public: bool update( diff --git a/libs/indidevice/property/indipropertyblob_p.h b/libs/indidevice/property/indipropertyblob_p.h index 2789caf6e4..61eb80e4a3 100644 --- a/libs/indidevice/property/indipropertyblob_p.h +++ b/libs/indidevice/property/indipropertyblob_p.h @@ -36,6 +36,7 @@ class PropertyBlobPrivate: public PropertyBasicPrivateTemplate virtual ~PropertyBlobPrivate(); public: + std::function deleter; }; From 377c1e50dbff3b2c494d908b1849252696d3a556 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Soja?= Date: Fri, 23 Jun 2023 13:47:48 +0200 Subject: [PATCH 2/2] free blob memory after object destroy --- libs/indidevice/basedevice.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libs/indidevice/basedevice.cpp b/libs/indidevice/basedevice.cpp index 97a400a941..27af5f894e 100644 --- a/libs/indidevice/basedevice.cpp +++ b/libs/indidevice/basedevice.cpp @@ -460,6 +460,15 @@ int BaseDevice::buildProp(const INDI::LilXmlElement &root, char *errmsg, bool is case INDI_BLOB: { INDI::PropertyBlob typedProperty {0}; + typedProperty.setBlobDeleter([](void *&blob) + { +#ifdef ENABLE_INDI_SHARED_MEMORY + IDSharedBlobFree(blob); +#else + free(blob); +#endif + blob = nullptr; + }); for (const auto &element : root.getElementsByTagName("defBLOB")) { INDI::WidgetViewBlob widget;