Skip to content

Bugfix: memory leak caused by variables not being deleted in end() #237

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

Merged
merged 6 commits into from
May 30, 2022
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
4 changes: 2 additions & 2 deletions src/BLECharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ BLECharacteristic::BLECharacteristic(const BLECharacteristic& other)

BLECharacteristic::~BLECharacteristic()
{
if (_local && _local->release() <= 0) {
if (_local && _local->release() == 0) {
delete _local;
}

if (_remote && _remote->release() <= 0) {
if (_remote && _remote->release() == 0) {
delete _remote;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/BLEDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ BLEDescriptor::BLEDescriptor(const BLEDescriptor& other)

BLEDescriptor::~BLEDescriptor()
{
if (_local && _local->release() <= 0) {
if (_local && _local->release() == 0) {
delete _local;
}

if (_remote && _remote->release() <= 0) {
if (_remote && _remote->release() == 0) {
delete _remote;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/BLEService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ BLEService::BLEService(const BLEService& other)

BLEService::~BLEService()
{
if (_local && _local->release() <= 0) {
if (_local && _local->release() == 0) {
delete _local;
}

if (_remote && _remote->release() <= 0) {
if (_remote && _remote->release() == 0) {
delete _remote;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/local/BLELocalCharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ BLELocalCharacteristic::~BLELocalCharacteristic()
for (unsigned int i = 0; i < descriptorCount(); i++) {
BLELocalDescriptor* d = descriptor(i);

if (d->release() <= 0) {
if (d->release() == 0) {
delete d;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/local/BLELocalService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ BLELocalService::~BLELocalService()
for (unsigned int i = 0; i < characteristicCount(); i++) {
BLELocalCharacteristic* c = characteristic(i);

if (c->release() <= 0) {
if (c->release() == 0) {
delete c;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/remote/BLERemoteCharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ BLERemoteCharacteristic::~BLERemoteCharacteristic()
for (unsigned int i = 0; i < descriptorCount(); i++) {
BLERemoteDescriptor* d = descriptor(i);

if (d->release() <= 0) {
if (d->release() == 0) {
delete d;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/remote/BLERemoteDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void BLERemoteDevice::clearServices()
for (unsigned int i = 0; i < serviceCount(); i++) {
BLERemoteService* s = service(i);

if (s->release() <= 0) {
if (s->release() == 0) {
delete s;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/remote/BLERemoteService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ BLERemoteService::~BLERemoteService()
for (unsigned int i = 0; i < characteristicCount(); i++) {
BLERemoteCharacteristic* c = characteristic(i);

if (c->release() <= 0) {
if (c->release() == 0) {
delete c;
}
}
Expand Down
21 changes: 18 additions & 3 deletions src/utility/GATT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ GATTClass::GATTClass() :

GATTClass::~GATTClass()
{
clearAttributes();
end();
}

void GATTClass::begin()
Expand Down Expand Up @@ -70,7 +70,22 @@ void GATTClass::begin()

void GATTClass::end()
{
_attributes.clear();
if (_genericAccessService->release() == 0)
delete(_genericAccessService);

if (_deviceNameCharacteristic->release() == 0)
delete(_deviceNameCharacteristic);

if (_appearanceCharacteristic->release() == 0)
delete(_appearanceCharacteristic);

if (_genericAttributeService->release() == 0)
delete(_genericAttributeService);

if (_servicesChangedCharacteristic->release() == 0)
delete(_servicesChangedCharacteristic);

clearAttributes();
}

void GATTClass::setDeviceName(const char* deviceName)
Expand Down Expand Up @@ -164,7 +179,7 @@ void GATTClass::clearAttributes()
for (unsigned int i = 0; i < attributeCount(); i++) {
BLELocalAttribute* a = attribute(i);

if (a->release() <= 0) {
if (a->release() == 0) {
delete a;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/utility/HCICordioTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ void HCICordioTransportClass::end()
delete bleLoopThread;
bleLoopThread = NULL;
}

#if !defined(ARDUINO_PORTENTA_H7_M4) && !defined(ARDUINO_PORTENTA_H7_M7) && !defined(ARDUINO_NICLA_VISION)
CordioHCIHook::getDriver().terminate();
#endif

_begun = false;
}
Expand Down