diff options
| author | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-12-23 17:37:15 +0000 | 
|---|---|---|
| committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-12-23 17:37:15 +0000 | 
| commit | 817ae5efdd5166eb1ebd0994af64fdc40d2d2f6b (patch) | |
| tree | 9f00ce1bf02426a8abf82255dfba7ad9d1431f2f | |
| parent | Add some missing features to resource handle for early release and reassignment (diff) | |
| download | libadhocutil-817ae5efdd5166eb1ebd0994af64fdc40d2d2f6b.tar.bz2 libadhocutil-817ae5efdd5166eb1ebd0994af64fdc40d2d2f6b.tar.xz libadhocutil-817ae5efdd5166eb1ebd0994af64fdc40d2d2f6b.zip  | |
Fix up and test behaviour of resource handles that are orphaned from a deleted pool
| -rw-r--r-- | libadhocutil/resourcePool.impl.h | 13 | ||||
| -rw-r--r-- | libadhocutil/unittests/testResourcePool.cpp | 14 | 
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;  | 
