Skip to content

Commit 05c68ed

Browse files
authored
Add clearAll parameter to deinit(), Fixes #12. (#94)
* Add clearAll parameter to deinit(), Fixes #12. Adds the ability to clear all resources consumed during BLE operation when deinitializing and shutting down BLE. Useful when BLE is used intermittently or in a task that initializes, performs operations then deinitializes. By setting the clearAll parameter to true all created BLE objects will be deleted, freeing the memory for other tasks. Warning: This will invalidate any pointers that may be referencing the deleted objects. * Fix deinit memory leaks. Add bool deleteCallbacks parameter to NimBLEServer::setCallbacks, if true (default) will delete the callback class when server is destructed. Delete scan results when scan is destructed.
1 parent 4dcb138 commit 05c68ed

File tree

6 files changed

+61
-5
lines changed

6 files changed

+61
-5
lines changed

src/NimBLEDevice.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,10 @@ void NimBLEDevice::stopAdvertising() {
539539

540540
/**
541541
* @brief Shutdown the NimBLE stack/controller.
542+
* @param [in] clearAll If true, deletes all server/advertising/scan/client objects after deinitializing.
543+
* @note If clearAll is true when called, any references to the created objects become invalid.
542544
*/
543-
/* STATIC */ void NimBLEDevice::deinit() {
545+
/* STATIC */ void NimBLEDevice::deinit(bool clearAll) {
544546
int ret = nimble_port_stop();
545547
if (ret == 0) {
546548
nimble_port_deinit();
@@ -552,6 +554,42 @@ void NimBLEDevice::stopAdvertising() {
552554

553555
initialized = false;
554556
m_synced = false;
557+
558+
if(clearAll) {
559+
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
560+
if(NimBLEDevice::m_pServer != nullptr) {
561+
delete NimBLEDevice::m_pServer;
562+
NimBLEDevice::m_pServer = nullptr;
563+
}
564+
#endif
565+
566+
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
567+
if(NimBLEDevice::m_bleAdvertising != nullptr) {
568+
delete NimBLEDevice::m_bleAdvertising;
569+
NimBLEDevice::m_bleAdvertising = nullptr;
570+
}
571+
#endif
572+
573+
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
574+
if(NimBLEDevice::m_pScan != nullptr) {
575+
delete NimBLEDevice::m_pScan;
576+
NimBLEDevice::m_pScan= nullptr;
577+
}
578+
#endif
579+
580+
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
581+
for(auto &it : m_cList) {
582+
deleteClient(it);
583+
m_cList.clear();
584+
}
585+
#endif
586+
587+
m_ignoreList.clear();
588+
589+
if(m_securityCallbacks != nullptr) {
590+
delete m_securityCallbacks;
591+
}
592+
}
555593
}
556594
} // deinit
557595

src/NimBLEDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ extern "C" void ble_store_config_init(void);
9191
class NimBLEDevice {
9292
public:
9393
static void init(const std::string &deviceName);
94-
static void deinit();
94+
static void deinit(bool clearAll = false);
9595
static bool getInitialized();
9696
static NimBLEAddress getAddress();
9797
static std::string toString();

src/NimBLEScan.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ NimBLEScan::NimBLEScan() {
4444
}
4545

4646

47+
/**
48+
* @brief Scan destructor, release any allocated resources.
49+
*/
50+
NimBLEScan::~NimBLEScan() {
51+
clearResults();
52+
}
53+
4754
/**
4855
* @brief Handle GAP events related to scans.
4956
* @param [in] event The event type for this event.

src/NimBLEScan.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ class NimBLEScan {
7777

7878

7979
private:
80-
NimBLEScan();
8180
friend class NimBLEDevice;
81+
82+
NimBLEScan();
83+
~NimBLEScan();
8284
static int handleGapEvent(ble_gap_event* event, void* arg);
8385
void onHostReset();
8486

src/NimBLEServer.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ NimBLEServer::NimBLEServer() {
4242
m_gattsStarted = false;
4343
m_advertiseOnDisconnect = true;
4444
m_svcChanged = false;
45+
m_deleteCallbacks = true;
4546
} // NimBLEServer
4647

4748

@@ -52,6 +53,10 @@ NimBLEServer::~NimBLEServer() {
5253
for(auto &it : m_svcVec) {
5354
delete it;
5455
}
56+
57+
if(m_deleteCallbacks && m_pServerCallbacks != &defaultCallbacks) {
58+
delete m_pServerCallbacks;
59+
}
5560
}
5661

5762

@@ -465,10 +470,12 @@ size_t NimBLEServer::getConnectedCount() {
465470
* events are detected.
466471
*
467472
* @param [in] pCallbacks The callbacks to be invoked.
473+
* @param [in] deleteCallbacks if true callback class will be deleted when server is destructed.
468474
*/
469-
void NimBLEServer::setCallbacks(NimBLEServerCallbacks* pCallbacks) {
475+
void NimBLEServer::setCallbacks(NimBLEServerCallbacks* pCallbacks, bool deleteCallbacks) {
470476
if (pCallbacks != nullptr){
471477
m_pServerCallbacks = pCallbacks;
478+
m_deleteCallbacks = deleteCallbacks;
472479
} else {
473480
m_pServerCallbacks = &defaultCallbacks;
474481
}

src/NimBLEServer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class NimBLEServer {
4444
void removeService(NimBLEService* service, bool deleteSvc = false);
4545
void addService(NimBLEService* service);
4646
NimBLEAdvertising* getAdvertising();
47-
void setCallbacks(NimBLEServerCallbacks* pCallbacks);
47+
void setCallbacks(NimBLEServerCallbacks* pCallbacks,
48+
bool deleteCallbacks = true);
4849
void startAdvertising();
4950
void stopAdvertising();
5051
void start();
@@ -70,6 +71,7 @@ class NimBLEServer {
7071
bool m_advertiseOnDisconnect;
7172
bool m_svcChanged;
7273
NimBLEServerCallbacks* m_pServerCallbacks;
74+
bool m_deleteCallbacks;
7375
std::vector<uint16_t> m_connectedPeersVec;
7476

7577
// uint16_t m_svcChgChrHdl; // Future use

0 commit comments

Comments
 (0)