Skip to content

Fix memory leak on end() -> begin() -> end() #260

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 1 commit into from
Sep 5, 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
7 changes: 7 additions & 0 deletions src/BLEService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ BLEService::BLEService(const BLEService& other)
}
}

void BLEService::clear()
{
if (_local) {
_local->clear();
}
}

BLEService::~BLEService()
{
if (_local && _local->release() == 0) {
Expand Down
1 change: 1 addition & 0 deletions src/BLEService.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class BLEService {
virtual ~BLEService();

const char* uuid() const;
void clear();

void addCharacteristic(BLECharacteristic& characteristic);

Expand Down
5 changes: 5 additions & 0 deletions src/local/BLELocalAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ int BLELocalAttribute::retain()
return _refCount;
}

bool BLELocalAttribute::active()
{
return _refCount > 0;
}

int BLELocalAttribute::release()
{
_refCount--;
Expand Down
1 change: 1 addition & 0 deletions src/local/BLELocalAttribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class BLELocalAttribute

int retain();
int release();
bool active();

protected:
friend class ATTClass;
Expand Down
1 change: 1 addition & 0 deletions src/local/BLELocalCharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ BLELocalCharacteristic::BLELocalCharacteristic(const char* uuid, uint16_t permis
if (permissions & (BLENotify | BLEIndicate)) {
BLELocalDescriptor* cccd = new BLELocalDescriptor("2902", (uint8_t*)&_cccdValue, sizeof(_cccdValue));

cccd->retain();
_descriptors.add(cccd);
}

Expand Down
9 changes: 7 additions & 2 deletions src/local/BLELocalService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ BLELocalService::BLELocalService(const char* uuid) :
{
}

void BLELocalService::clear() {
_characteristics.clear();
_startHandle = 0;
_endHandle = 0;
}

BLELocalService::~BLELocalService()
{
for (unsigned int i = 0; i < characteristicCount(); i++) {
Expand All @@ -37,8 +43,7 @@ BLELocalService::~BLELocalService()
delete c;
}
}

_characteristics.clear();
clear();
}

enum BLEAttributeType BLELocalService::type() const
Expand Down
1 change: 1 addition & 0 deletions src/local/BLELocalService.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class BLELocalService : public BLELocalAttribute {
virtual enum BLEAttributeType type() const;

void addCharacteristic(BLECharacteristic& characteristic);
void clear();

protected:
friend class ATTClass;
Expand Down
9 changes: 8 additions & 1 deletion src/utility/GATT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ void GATTClass::addService(BLELocalService* service)
{
service->retain();
_attributes.add(service);
_services.add(service);

uint16_t startHandle = attributeCount();

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

// add the characteristic again to make space of the characteristic value handle
characteristic->retain();
_attributes.add(characteristic);

for (unsigned int j = 0; j < characteristic->descriptorCount(); j++) {
Expand All @@ -183,8 +185,13 @@ void GATTClass::clearAttributes()
delete a;
}
}

_attributes.clear();

for (unsigned int i = 0; i < _services.size(); i++) {
_services.get(i)->clear();
}
_services.clear();

}

#if !defined(FAKE_GATT)
Expand Down
1 change: 1 addition & 0 deletions src/utility/GATT.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class GATTClass {

private:
BLELinkedList<BLELocalAttribute*> _attributes;
BLELinkedList<BLELocalService*> _services;

BLELocalService* _genericAccessService;
BLELocalCharacteristic* _deviceNameCharacteristic;
Expand Down