Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix leaks last blob on driver restart #1909

Merged
merged 2 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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