From e84963b9bb7bd383dd7f8bbb08b261d247b0332f Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Mon, 19 Oct 2020 13:21:08 +0200 Subject: DBTDevice::dtor: Don't recurse back into remove() -> adapter removeDevice(), that is how it potentially has been deleted! This fix also gives us the opportunity back to use a simple mutex for mtx_sharedDevices. Also add dedicated close() for better leak testing. --- src/direct_bt/DBTAdapter.cpp | 24 +++++++++++++++--------- src/direct_bt/DBTDevice.cpp | 1 - 2 files changed, 15 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/direct_bt/DBTAdapter.cpp b/src/direct_bt/DBTAdapter.cpp index 198dd8f2..5f90279c 100644 --- a/src/direct_bt/DBTAdapter.cpp +++ b/src/direct_bt/DBTAdapter.cpp @@ -225,6 +225,12 @@ DBTAdapter::DBTAdapter(const int _dev_id) noexcept DBTAdapter::~DBTAdapter() noexcept { DBG_PRINT("DBTAdapter::dtor: ... %p %s", this, toString().c_str()); + close(); + DBG_PRINT("DBTAdapter::dtor: XXX"); +} + +void DBTAdapter::close() noexcept { + DBG_PRINT("DBTAdapter::close: ... %p %s", this, toString().c_str()); keepDiscoveringAlive = false; // mute all listener first { @@ -235,9 +241,9 @@ DBTAdapter::~DBTAdapter() noexcept { poweredOff(); - DBG_PRINT("DBTAdapter::dtor: closeHCI: ..."); + DBG_PRINT("DBTAdapter::close: closeHCI: ..."); hci.close(); - DBG_PRINT("DBTAdapter::dtor: closeHCI: XXX"); + DBG_PRINT("DBTAdapter::close: closeHCI: XXX"); { const std::lock_guard lock(mtx_discoveredDevices); // RAII-style acquire and relinquish via destructor @@ -248,10 +254,10 @@ DBTAdapter::~DBTAdapter() noexcept { connectedDevices.clear();; } { - const std::lock_guard lock(mtx_sharedDevices); // RAII-style acquire and relinquish via destructor + const std::lock_guard lock(mtx_sharedDevices); // RAII-style acquire and relinquish via destructor sharedDevices.clear(); } - DBG_PRINT("DBTAdapter::dtor: XXX"); + DBG_PRINT("DBTAdapter::close: XXX"); } void DBTAdapter::poweredOff() noexcept { @@ -271,7 +277,7 @@ void DBTAdapter::poweredOff() noexcept { void DBTAdapter::printSharedPtrListOfDevices() noexcept { { - const std::lock_guard lock0(mtx_sharedDevices); + const std::lock_guard lock0(mtx_sharedDevices); jau::printSharedPtrList("SharedDevices", sharedDevices); } { @@ -621,7 +627,7 @@ std::vector> DBTAdapter::getDiscoveredDevices() const } bool DBTAdapter::addSharedDevice(std::shared_ptr const &device) noexcept { - const std::lock_guard lock(mtx_sharedDevices); // RAII-style acquire and relinquish via destructor + const std::lock_guard lock(mtx_sharedDevices); // RAII-style acquire and relinquish via destructor if( nullptr != findDevice(sharedDevices, *device) ) { // already shared return false; @@ -631,12 +637,12 @@ bool DBTAdapter::addSharedDevice(std::shared_ptr const &device) noexc } std::shared_ptr DBTAdapter::getSharedDevice(const DBTDevice & device) noexcept { - const std::lock_guard lock(mtx_sharedDevices); // RAII-style acquire and relinquish via destructor + const std::lock_guard lock(mtx_sharedDevices); // RAII-style acquire and relinquish via destructor return findDevice(sharedDevices, device); } void DBTAdapter::removeSharedDevice(const DBTDevice & device) noexcept { - const std::lock_guard lock(mtx_sharedDevices); // RAII-style acquire and relinquish via destructor + const std::lock_guard lock(mtx_sharedDevices); // RAII-style acquire and relinquish via destructor for (auto it = sharedDevices.begin(); it != sharedDevices.end(); ) { if ( nullptr != *it && device == **it ) { it = sharedDevices.erase(it); @@ -648,7 +654,7 @@ void DBTAdapter::removeSharedDevice(const DBTDevice & device) noexcept { } std::shared_ptr DBTAdapter::findSharedDevice (EUI48 const & mac, const BDAddressType macType) noexcept { - const std::lock_guard lock(mtx_sharedDevices); // RAII-style acquire and relinquish via destructor + const std::lock_guard lock(mtx_sharedDevices); // RAII-style acquire and relinquish via destructor return findDevice(sharedDevices, mac, macType); } diff --git a/src/direct_bt/DBTDevice.cpp b/src/direct_bt/DBTDevice.cpp index 57fe251c..1e3ddfd8 100644 --- a/src/direct_bt/DBTDevice.cpp +++ b/src/direct_bt/DBTDevice.cpp @@ -74,7 +74,6 @@ DBTDevice::DBTDevice(DBTAdapter & a, EInfoReport const & r) DBTDevice::~DBTDevice() noexcept { DBG_PRINT("DBTDevice::dtor: ... %p %s", this, getAddressString().c_str()); - remove(); advServices.clear(); advMSD = nullptr; DBG_PRINT("DBTDevice::dtor: XXX %p %s", this, getAddressString().c_str()); -- cgit v1.2.3