Skip to content

Commit

Permalink
Merge pull request #237 from alranel/bugfix/192-leak
Browse files Browse the repository at this point in the history
Bugfix: memory leak caused by variables not being deleted in end()
  • Loading branch information
facchinm authored May 30, 2022
2 parents f2f73e8 + afccc49 commit 3003f12
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 14 deletions.
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

0 comments on commit 3003f12

Please sign in to comment.