Skip to content

Commit 5954d96

Browse files
authored
Merge pull request arduino-libraries#260 from facchinm/fix_end_hang
Fix memory leak on end() -> begin() -> end()
2 parents b5fc368 + 6e97bf2 commit 5954d96

File tree

9 files changed

+32
-3
lines changed

9 files changed

+32
-3
lines changed

src/BLEService.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ BLEService::BLEService(const BLEService& other)
6363
}
6464
}
6565

66+
void BLEService::clear()
67+
{
68+
if (_local) {
69+
_local->clear();
70+
}
71+
}
72+
6673
BLEService::~BLEService()
6774
{
6875
if (_local && _local->release() == 0) {

src/BLEService.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class BLEService {
3333
virtual ~BLEService();
3434

3535
const char* uuid() const;
36+
void clear();
3637

3738
void addCharacteristic(BLECharacteristic& characteristic);
3839

src/local/BLELocalAttribute.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ int BLELocalAttribute::retain()
5656
return _refCount;
5757
}
5858

59+
bool BLELocalAttribute::active()
60+
{
61+
return _refCount > 0;
62+
}
63+
5964
int BLELocalAttribute::release()
6065
{
6166
_refCount--;

src/local/BLELocalAttribute.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class BLELocalAttribute
4444

4545
int retain();
4646
int release();
47+
bool active();
4748

4849
protected:
4950
friend class ATTClass;

src/local/BLELocalCharacteristic.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ BLELocalCharacteristic::BLELocalCharacteristic(const char* uuid, uint16_t permis
4646
if (permissions & (BLENotify | BLEIndicate)) {
4747
BLELocalDescriptor* cccd = new BLELocalDescriptor("2902", (uint8_t*)&_cccdValue, sizeof(_cccdValue));
4848

49+
cccd->retain();
4950
_descriptors.add(cccd);
5051
}
5152

src/local/BLELocalService.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ BLELocalService::BLELocalService(const char* uuid) :
2828
{
2929
}
3030

31+
void BLELocalService::clear() {
32+
_characteristics.clear();
33+
_startHandle = 0;
34+
_endHandle = 0;
35+
}
36+
3137
BLELocalService::~BLELocalService()
3238
{
3339
for (unsigned int i = 0; i < characteristicCount(); i++) {
@@ -37,8 +43,7 @@ BLELocalService::~BLELocalService()
3743
delete c;
3844
}
3945
}
40-
41-
_characteristics.clear();
46+
clear();
4247
}
4348

4449
enum BLEAttributeType BLELocalService::type() const

src/local/BLELocalService.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class BLELocalService : public BLELocalAttribute {
3636
virtual enum BLEAttributeType type() const;
3737

3838
void addCharacteristic(BLECharacteristic& characteristic);
39+
void clear();
3940

4041
protected:
4142
friend class ATTClass;

src/utility/GATT.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ void GATTClass::addService(BLELocalService* service)
149149
{
150150
service->retain();
151151
_attributes.add(service);
152+
_services.add(service);
152153

153154
uint16_t startHandle = attributeCount();
154155

@@ -160,6 +161,7 @@ void GATTClass::addService(BLELocalService* service)
160161
characteristic->setHandle(attributeCount());
161162

162163
// add the characteristic again to make space of the characteristic value handle
164+
characteristic->retain();
163165
_attributes.add(characteristic);
164166

165167
for (unsigned int j = 0; j < characteristic->descriptorCount(); j++) {
@@ -183,8 +185,13 @@ void GATTClass::clearAttributes()
183185
delete a;
184186
}
185187
}
186-
187188
_attributes.clear();
189+
190+
for (unsigned int i = 0; i < _services.size(); i++) {
191+
_services.get(i)->clear();
192+
}
193+
_services.clear();
194+
188195
}
189196

190197
#if !defined(FAKE_GATT)

src/utility/GATT.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class GATTClass {
5959

6060
private:
6161
BLELinkedList<BLELocalAttribute*> _attributes;
62+
BLELinkedList<BLELocalService*> _services;
6263

6364
BLELocalService* _genericAccessService;
6465
BLELocalCharacteristic* _deviceNameCharacteristic;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy