From 73f02770428e3aa3c5762bce3751a9e76b38354a Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Fri, 6 Sep 2024 08:52:04 -0500 Subject: [PATCH] fix(NimBLEDevice): clear all before port_deinit to prevent crash --- src/NimBLEDevice.cpp | 62 ++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/src/NimBLEDevice.cpp b/src/NimBLEDevice.cpp index 74e3fd1..00ff8f4 100644 --- a/src/NimBLEDevice.cpp +++ b/src/NimBLEDevice.cpp @@ -920,6 +920,36 @@ void NimBLEDevice::init(const std::string &deviceName) { */ /* STATIC */ void NimBLEDevice::deinit(bool clearAll) { + if(clearAll) { +#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) + if(NimBLEDevice::m_pServer != nullptr) { + delete NimBLEDevice::m_pServer; + NimBLEDevice::m_pServer = nullptr; + } +#endif + +#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) + if(NimBLEDevice::m_bleAdvertising != nullptr) { + delete NimBLEDevice::m_bleAdvertising; + NimBLEDevice::m_bleAdvertising = nullptr; + } +#endif + +#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) + if(NimBLEDevice::m_pScan != nullptr) { + delete NimBLEDevice::m_pScan; + NimBLEDevice::m_pScan= nullptr; + } +#endif + +#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL) + for(auto clt : m_pClients) { + deleteClient(clt); + } +#endif + m_ignoreList.clear(); + } + int ret = nimble_port_stop(); if (ret == 0) { nimble_port_deinit(); @@ -933,36 +963,6 @@ void NimBLEDevice::deinit(bool clearAll) { #endif initialized = false; m_synced = false; - - if(clearAll) { -#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) - if(NimBLEDevice::m_pServer != nullptr) { - delete NimBLEDevice::m_pServer; - NimBLEDevice::m_pServer = nullptr; - } -#endif - -#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) - if(NimBLEDevice::m_bleAdvertising != nullptr) { - delete NimBLEDevice::m_bleAdvertising; - NimBLEDevice::m_bleAdvertising = nullptr; - } -#endif - -#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) - if(NimBLEDevice::m_pScan != nullptr) { - delete NimBLEDevice::m_pScan; - NimBLEDevice::m_pScan= nullptr; - } -#endif - -#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL) - for(auto clt : m_pClients) { - deleteClient(clt); - } -#endif - m_ignoreList.clear(); - } } } // deinit @@ -1243,4 +1243,4 @@ void nimble_cpp_assert(const char *file, unsigned line) { } #endif // CONFIG_NIMBLE_CPP_DEBUG_ASSERT_ENABLED -#endif // CONFIG_BT_ENABLED \ No newline at end of file +#endif // CONFIG_BT_ENABLED