From 58136fca11465a5b6703b386d9b1f0f1c13ccedf Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Thu, 12 May 2022 10:18:17 +0200 Subject: [PATCH 1/6] Bugfix: memory leak caused by variables not being deleted in end(). #192 --- src/utility/GATT.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/utility/GATT.cpp b/src/utility/GATT.cpp index 373213b9..50af4f6c 100644 --- a/src/utility/GATT.cpp +++ b/src/utility/GATT.cpp @@ -70,7 +70,12 @@ void GATTClass::begin() void GATTClass::end() { - _attributes.clear(); + delete(_genericAccessService); + delete(_deviceNameCharacteristic); + delete(_appearanceCharacteristic); + delete(_genericAttributeService); + delete(_servicesChangedCharacteristic); + clearAttributes(); } void GATTClass::setDeviceName(const char* deviceName) From 3c6a53ae7856ccf7189df7adc9f38079c8371432 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Thu, 12 May 2022 14:01:21 +0200 Subject: [PATCH 2/6] Respect refcounting before freeing dynamically allocated variables --- src/utility/GATT.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/utility/GATT.cpp b/src/utility/GATT.cpp index 50af4f6c..5db7ec55 100644 --- a/src/utility/GATT.cpp +++ b/src/utility/GATT.cpp @@ -70,11 +70,21 @@ void GATTClass::begin() void GATTClass::end() { - delete(_genericAccessService); - delete(_deviceNameCharacteristic); - delete(_appearanceCharacteristic); - delete(_genericAttributeService); - delete(_servicesChangedCharacteristic); + 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(); } From 122319959ddeb317b2e76ffc4c350f4f2f5804d4 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Thu, 12 May 2022 18:12:46 +0200 Subject: [PATCH 3/6] Call end() upon destruction --- src/utility/GATT.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utility/GATT.cpp b/src/utility/GATT.cpp index 5db7ec55..3874fb6f 100644 --- a/src/utility/GATT.cpp +++ b/src/utility/GATT.cpp @@ -38,7 +38,7 @@ GATTClass::GATTClass() : GATTClass::~GATTClass() { - clearAttributes(); + end(); } void GATTClass::begin() From f24ba26576fdb74e6be4ec9f704042f059bdd6e6 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Thu, 12 May 2022 15:12:16 +0200 Subject: [PATCH 4/6] Prevent double object deletion --- src/BLECharacteristic.cpp | 4 ++-- src/BLEDescriptor.cpp | 4 ++-- src/BLEService.cpp | 4 ++-- src/local/BLELocalCharacteristic.cpp | 2 +- src/local/BLELocalService.cpp | 2 +- src/remote/BLERemoteCharacteristic.cpp | 2 +- src/remote/BLERemoteDevice.cpp | 2 +- src/remote/BLERemoteService.cpp | 2 +- src/utility/GATT.cpp | 2 +- 9 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/BLECharacteristic.cpp b/src/BLECharacteristic.cpp index 1cfbf489..cb9783ce 100644 --- a/src/BLECharacteristic.cpp +++ b/src/BLECharacteristic.cpp @@ -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; } } diff --git a/src/BLEDescriptor.cpp b/src/BLEDescriptor.cpp index 7a6736b0..366b89aa 100644 --- a/src/BLEDescriptor.cpp +++ b/src/BLEDescriptor.cpp @@ -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; } } diff --git a/src/BLEService.cpp b/src/BLEService.cpp index 7b5df148..b3f33739 100644 --- a/src/BLEService.cpp +++ b/src/BLEService.cpp @@ -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; } } diff --git a/src/local/BLELocalCharacteristic.cpp b/src/local/BLELocalCharacteristic.cpp index 2cd801b2..63208b05 100644 --- a/src/local/BLELocalCharacteristic.cpp +++ b/src/local/BLELocalCharacteristic.cpp @@ -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; } } diff --git a/src/local/BLELocalService.cpp b/src/local/BLELocalService.cpp index 442c5422..58957342 100644 --- a/src/local/BLELocalService.cpp +++ b/src/local/BLELocalService.cpp @@ -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; } } diff --git a/src/remote/BLERemoteCharacteristic.cpp b/src/remote/BLERemoteCharacteristic.cpp index e8ac733f..454438b6 100644 --- a/src/remote/BLERemoteCharacteristic.cpp +++ b/src/remote/BLERemoteCharacteristic.cpp @@ -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; } } diff --git a/src/remote/BLERemoteDevice.cpp b/src/remote/BLERemoteDevice.cpp index 5a49f26f..1a4a67ab 100644 --- a/src/remote/BLERemoteDevice.cpp +++ b/src/remote/BLERemoteDevice.cpp @@ -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; } } diff --git a/src/remote/BLERemoteService.cpp b/src/remote/BLERemoteService.cpp index fd5c0ba6..f7461290 100644 --- a/src/remote/BLERemoteService.cpp +++ b/src/remote/BLERemoteService.cpp @@ -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; } } diff --git a/src/utility/GATT.cpp b/src/utility/GATT.cpp index 3874fb6f..35adb5f7 100644 --- a/src/utility/GATT.cpp +++ b/src/utility/GATT.cpp @@ -179,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; } } From 2b72c948b3f2da4287ce69488da9804bb4bb8e5f Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 23 May 2022 18:11:19 +0200 Subject: [PATCH 5/6] Don't double-free characteristics --- src/utility/GATT.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/utility/GATT.cpp b/src/utility/GATT.cpp index 35adb5f7..98d5f4ad 100644 --- a/src/utility/GATT.cpp +++ b/src/utility/GATT.cpp @@ -70,19 +70,19 @@ void GATTClass::begin() void GATTClass::end() { - if (_genericAccessService->release() <= 0) + if (_genericAccessService->release() == 0) delete(_genericAccessService); - if (_deviceNameCharacteristic->release() <= 0) + if (_deviceNameCharacteristic->release() == 0) delete(_deviceNameCharacteristic); - if (_appearanceCharacteristic->release() <= 0) + if (_appearanceCharacteristic->release() == 0) delete(_appearanceCharacteristic); - if (_genericAttributeService->release() <= 0) + if (_genericAttributeService->release() == 0) delete(_genericAttributeService); - if (_servicesChangedCharacteristic->release() <= 0) + if (_servicesChangedCharacteristic->release() == 0) delete(_servicesChangedCharacteristic); clearAttributes(); From afccc49f94fcac2d961e74309b100ba7c9e961d1 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 23 May 2022 18:12:01 +0200 Subject: [PATCH 6/6] Don't terminate CordioHCIHook::getDriver() on end() for Murata chips --- src/utility/HCICordioTransport.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/utility/HCICordioTransport.cpp b/src/utility/HCICordioTransport.cpp index 28eb97be..4dc8d45e 100644 --- a/src/utility/HCICordioTransport.cpp +++ b/src/utility/HCICordioTransport.cpp @@ -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; }