diff options
author | Sven Gothel <[email protected]> | 2020-10-27 05:20:01 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-10-27 05:20:01 +0100 |
commit | 034d70013b8d17f7b66a7ceb405d7597f9e5b91d (patch) | |
tree | 9d80d55d21e530dcbf2b634e3f940c8d2c5a8b72 /examples | |
parent | 2bddceb8df4dc38d89ce448ab1c2f3048ec22074 (diff) |
Resolve adapter lifecycle issue: Have Add/remove callbacks in dedicated thread, drop DBTAdapter::close() call; Fix DBTManager.java COW adapters usage
Resolve adapter lifecycle issue: No use after free:
- Perform add/remove callbacks in dedicated thread
Have remove also being called from a dedicated thread in DBTManager (like add),
hence calling down into the MgmtEventCallbacks + User callbacks from here on sequentially.
- drop erratic DBTAdapter::close() call
DBTAdapter shall not react itself to its removal, since user will
- DBTAdapter dtor and close() shall just return if !isValid
Fix DBTManager.java COW adapters usage:
- Benefit from all locked-COW [write-]operation
via removeIf, forEach
- Otherwise use iterators, which operate on the snapshot
Diffstat (limited to 'examples')
-rw-r--r-- | examples/direct_bt_scanner10/dbt_scanner10.cpp | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/examples/direct_bt_scanner10/dbt_scanner10.cpp b/examples/direct_bt_scanner10/dbt_scanner10.cpp index 4c95f823..30bd3e81 100644 --- a/examples/direct_bt_scanner10/dbt_scanner10.cpp +++ b/examples/direct_bt_scanner10/dbt_scanner10.cpp @@ -565,22 +565,21 @@ static std::shared_ptr<DBTAdapter> getAdapter(const uint16_t dev_id) { return *it; } } -static int removeAdapter(const uint16_t dev_id) { +static std::shared_ptr<DBTAdapter> removeAdapter(const uint16_t dev_id) { + std::shared_ptr<DBTAdapter> res = nullptr; const std::lock_guard<std::recursive_mutex> lock(adapterList.get_write_mutex()); std::shared_ptr<std::vector<std::shared_ptr<DBTAdapter>>> store = adapterList.copy_store(); - int count = 0; for(auto it = store->begin(); it != store->end(); ) { if ( (*it)->dev_id == dev_id ) { + res = *it; it = store->erase(it); - count++; + adapterList.set_store(std::move(store)); + return res; } else { ++it; } } - if( 0 < count ) { - adapterList.set_store(std::move(store)); - } - return count; + return nullptr; } static bool myChangedAdapterSetFunc(const bool added, const AdapterInfo& adapterInfo) { @@ -600,8 +599,13 @@ static bool myChangedAdapterSetFunc(const bool added, const AdapterInfo& adapter } } } else { - const int count = removeAdapter(adapterInfo.dev_id); - fprintf(stderr, "****** Adapter REMOVED: count %d, %s\n", count, adapterInfo.toString().c_str()); + std::shared_ptr<DBTAdapter> removed = removeAdapter(adapterInfo.dev_id); + if( nullptr != removed ) { + fprintf(stderr, "****** Adapter REMOVED: %s\n", removed->toString().c_str()); + removed->close(); + } else { + fprintf(stderr, "****** Adapter REMOVED: Not found %s\n", adapterInfo.toString().c_str()); + } } return true; } |