From dbca93783599dae130a9d83a6e80a3faace12e89 Mon Sep 17 00:00:00 2001 From: Kezhu Wang Date: Tue, 3 Jun 2014 17:58:09 +0800 Subject: [PATCH 1/4] add test case to break buggy code --- tests/cpp-tests/Classes/ReleasePoolTest/ReleasePoolTest.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/cpp-tests/Classes/ReleasePoolTest/ReleasePoolTest.cpp b/tests/cpp-tests/Classes/ReleasePoolTest/ReleasePoolTest.cpp index 14e9b8dc6a..70e54dbc49 100644 --- a/tests/cpp-tests/Classes/ReleasePoolTest/ReleasePoolTest.cpp +++ b/tests/cpp-tests/Classes/ReleasePoolTest/ReleasePoolTest.cpp @@ -75,6 +75,11 @@ void ReleasePoolTestScene::runThisTest() } // object in pool2 should be released + + { + new AutoreleasePool; + PoolManager::destroyInstance(); + } Director::getInstance()->replaceScene(this); } From 1eb991df5976616dfbc1dfae884f8f444e351534 Mon Sep 17 00:00:00 2001 From: Kezhu Wang Date: Tue, 3 Jun 2014 18:57:52 +0800 Subject: [PATCH 2/4] bugfix related to AutoreleasePool's PoolManager * duplicated AutoreleasePool added to PoolManager in PoolManager::getInstance() * redundant pop_back() in PoolManager::~PoolManager() --- cocos/base/CCAutoreleasePool.cpp | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/cocos/base/CCAutoreleasePool.cpp b/cocos/base/CCAutoreleasePool.cpp index d5e35a01c4..25478aec5e 100644 --- a/cocos/base/CCAutoreleasePool.cpp +++ b/cocos/base/CCAutoreleasePool.cpp @@ -111,8 +111,7 @@ PoolManager* PoolManager::getInstance() { s_singleInstance = new PoolManager(); // Add the first auto release pool - s_singleInstance->_curReleasePool = new AutoreleasePool("cocos2d autorelease pool"); - s_singleInstance->_releasePoolStack.push_back(s_singleInstance->_curReleasePool); + new AutoreleasePool("cocos2d autorelease pool"); } return s_singleInstance; } @@ -134,7 +133,6 @@ PoolManager::~PoolManager() while (!_releasePoolStack.empty()) { AutoreleasePool* pool = _releasePoolStack.back(); - _releasePoolStack.pop_back(); delete pool; } @@ -164,16 +162,9 @@ void PoolManager::push(AutoreleasePool *pool) void PoolManager::pop() { - // Can not pop the pool that created by engine - CC_ASSERT(_releasePoolStack.size() >= 1); - + CC_ASSERT(!_releasePoolStack.empty()); _releasePoolStack.pop_back(); - - // Should update _curReleasePool if a temple pool is released - if (_releasePoolStack.size() > 1) - { - _curReleasePool = _releasePoolStack.back(); - } + _curReleasePool = _releasePoolStack.empty() ? nullptr : _releasePoolStack.back(); } NS_CC_END From f1ce9e032d132b5ae788fca7716576b18c32b91c Mon Sep 17 00:00:00 2001 From: Kezhu Wang Date: Tue, 3 Jun 2014 19:08:28 +0800 Subject: [PATCH 3/4] remove unnecessary cache-variable --- cocos/base/CCAutoreleasePool.cpp | 4 +--- cocos/base/CCAutoreleasePool.h | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/cocos/base/CCAutoreleasePool.cpp b/cocos/base/CCAutoreleasePool.cpp index 25478aec5e..7656cd7921 100644 --- a/cocos/base/CCAutoreleasePool.cpp +++ b/cocos/base/CCAutoreleasePool.cpp @@ -141,7 +141,7 @@ PoolManager::~PoolManager() AutoreleasePool* PoolManager::getCurrentPool() const { - return _curReleasePool; + return _releasePoolStack.back(); } bool PoolManager::isObjectInPools(Ref* obj) const @@ -157,14 +157,12 @@ bool PoolManager::isObjectInPools(Ref* obj) const void PoolManager::push(AutoreleasePool *pool) { _releasePoolStack.push_back(pool); - _curReleasePool = pool; } void PoolManager::pop() { CC_ASSERT(!_releasePoolStack.empty()); _releasePoolStack.pop_back(); - _curReleasePool = _releasePoolStack.empty() ? nullptr : _releasePoolStack.back(); } NS_CC_END diff --git a/cocos/base/CCAutoreleasePool.h b/cocos/base/CCAutoreleasePool.h index 8ceaa5c879..81ddccf86a 100644 --- a/cocos/base/CCAutoreleasePool.h +++ b/cocos/base/CCAutoreleasePool.h @@ -164,7 +164,6 @@ private: static PoolManager* s_singleInstance; std::deque _releasePoolStack; - AutoreleasePool *_curReleasePool; }; // end of base_nodes group From eb8e08ccddccb6c8c19b0310197d84c34283b021 Mon Sep 17 00:00:00 2001 From: Kezhu Wang Date: Tue, 3 Jun 2014 19:09:48 +0800 Subject: [PATCH 4/4] prefer std::vector to other containers --- cocos/base/CCAutoreleasePool.cpp | 1 + cocos/base/CCAutoreleasePool.h | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/cocos/base/CCAutoreleasePool.cpp b/cocos/base/CCAutoreleasePool.cpp index 7656cd7921..06bedcd948 100644 --- a/cocos/base/CCAutoreleasePool.cpp +++ b/cocos/base/CCAutoreleasePool.cpp @@ -124,6 +124,7 @@ void PoolManager::destroyInstance() PoolManager::PoolManager() { + _releasePoolStack.reserve(10); } PoolManager::~PoolManager() diff --git a/cocos/base/CCAutoreleasePool.h b/cocos/base/CCAutoreleasePool.h index 81ddccf86a..6d1134a77e 100644 --- a/cocos/base/CCAutoreleasePool.h +++ b/cocos/base/CCAutoreleasePool.h @@ -163,7 +163,7 @@ private: static PoolManager* s_singleInstance; - std::deque _releasePoolStack; + std::vector _releasePoolStack; }; // end of base_nodes group