Skip to content

Commit

Permalink
Fix leaks last blob on driver restart (#1909)
Browse files Browse the repository at this point in the history
  • Loading branch information
pawel-soja authored Jul 28, 2023
1 parent 6aabc08 commit 4224155
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
9 changes: 9 additions & 0 deletions libs/indidevice/basedevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 16 additions & 1 deletion libs/indidevice/property/indipropertyblob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<IBLOB>(*new PropertyBlobPrivate(count))
Expand All @@ -40,6 +49,12 @@ PropertyBlob::PropertyBlob(INDI::Property property)
PropertyBlob::~PropertyBlob()
{ }

void PropertyBlob::setBlobDeleter(const std::function<void(void *&)> &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
Expand Down
10 changes: 10 additions & 0 deletions libs/indidevice/property/indipropertyblob.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ class PropertyBlob: public INDI::PropertyBasic<IBLOB>
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<void(void *&)> &deleter);

public:
bool update(
Expand Down
1 change: 1 addition & 0 deletions libs/indidevice/property/indipropertyblob_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class PropertyBlobPrivate: public PropertyBasicPrivateTemplate<IBLOB>
virtual ~PropertyBlobPrivate();

public:
std::function<void(void *&)> deleter;

};

Expand Down

0 comments on commit 4224155

Please sign in to comment.