diff options
author | Jon Szymaniak <jon.szymaniak@gmail.com> | 2014-03-07 01:44:17 -0500 |
---|---|---|
committer | Dimitri Stolnikov <horiz0n@gmx.net> | 2014-05-03 18:27:47 +0200 |
commit | 2ae3fdbc220df4896a87f9fdb1bacc8a6c08c849 (patch) | |
tree | 3a7b89b9694b6444550e228d16747d25dc7e6bdb | |
parent | 79ad6f6a7603f48d8b684e88d2e6e55a366693c3 (diff) |
bladerf: Fixed bug in cached device cleanup
A couple issues were present in bladerf_common::close, which caused
entries in the _devs list (our "device cache") to not be removed. This
would result in a stale device handle being used upon attempting to
reopen the device.
Two issues were associated with this bug:
- The weak_ptr expired() conditional was incorrect; the logic was
inverted.
- The list item removal and iterator increment was done incorrectly
and would result in a crash after the first item was fixed.
-rw-r--r-- | lib/bladerf/bladerf_common.cc | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/lib/bladerf/bladerf_common.cc b/lib/bladerf/bladerf_common.cc index 23035e1..fd0182f 100644 --- a/lib/bladerf/bladerf_common.cc +++ b/lib/bladerf/bladerf_common.cc @@ -75,14 +75,20 @@ bladerf_sptr bladerf_common:: get_cached_device(struct bladerf_devinfo devinfo) return bladerf_sptr(); } +/* This is called when a bladerf_sptr hits a refcount of 0 */ void bladerf_common::close(void* dev) { boost::unique_lock<boost::mutex> lock(_devs_mutex); - std::list<boost::weak_ptr<struct bladerf> >::iterator it; - for (it = _devs.begin(); it != _devs.end(); ++it) - if ( (*it).expired() == 0 ) - _devs.erase(it); + /* Prune expired entries from device cache */ + std::list<boost::weak_ptr<struct bladerf> >::iterator it(_devs.begin()); + while ( it != _devs.end() ) { + if ( (*it).expired() ) { + it = _devs.erase(it); + } else { + ++it; + } + } bladerf_close((struct bladerf *)dev); } |