summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libadhocutil/resourcePool.impl.h13
-rw-r--r--libadhocutil/unittests/testResourcePool.cpp14
2 files changed, 22 insertions, 5 deletions
diff --git a/libadhocutil/resourcePool.impl.h b/libadhocutil/resourcePool.impl.h
index 269cd00..b421e62 100644
--- a/libadhocutil/resourcePool.impl.h
+++ b/libadhocutil/resourcePool.impl.h
@@ -96,11 +96,13 @@ namespace AdHoc {
{
ASSERT(resource);
if (!--boost::get<2>(*resource)) {
- if (std::uncaught_exception()) {
- boost::get<1>(*resource)->discard(boost::get<0>(*resource));
- }
- else {
- boost::get<1>(*resource)->putBack(boost::get<0>(*resource));
+ if (auto & pool = boost::get<1>(*resource)) {
+ if (std::uncaught_exception()) {
+ pool->discard(boost::get<0>(*resource));
+ }
+ else {
+ pool->putBack(boost::get<0>(*resource));
+ }
}
delete resource;
}
@@ -126,6 +128,7 @@ namespace AdHoc {
}
for (auto & r : inUse) {
destroyResource(boost::get<0>(*r.second));
+ boost::get<1>(*r.second) = nullptr;
}
}
diff --git a/libadhocutil/unittests/testResourcePool.cpp b/libadhocutil/unittests/testResourcePool.cpp
index 411f1bb..3af149b 100644
--- a/libadhocutil/unittests/testResourcePool.cpp
+++ b/libadhocutil/unittests/testResourcePool.cpp
@@ -11,6 +11,8 @@ class MockResource {
MockResource(const MockResource &) = delete;
void operator=(const MockResource &) = delete;
+ bool valid() const { return true; }
+
static std::atomic<unsigned int> count;
};
@@ -90,6 +92,18 @@ BOOST_AUTO_TEST_CASE ( get )
BOOST_REQUIRE_EQUAL(0, MockResource::count);
}
+BOOST_AUTO_TEST_CASE( destroyPoolWhenInUse )
+{
+ auto rp = new TRP();
+ auto rh1 = rp->get();
+ auto rh2 = rp->get();
+ auto rh3 = rh1;
+ delete rp;
+ BOOST_REQUIRE(rh1->valid());
+ BOOST_REQUIRE(rh2->valid());
+ BOOST_REQUIRE(rh3->valid());
+}
+
BOOST_AUTO_TEST_CASE ( getMine )
{
TRP pool;