From 54b4c690d15c4bdf4f25469ceac81b56d19afc8d Mon Sep 17 00:00:00 2001 From: James Chen Date: Mon, 8 Oct 2012 13:41:48 +0800 Subject: [PATCH 1/2] fixed #1494: Missing removing auto-release object from AutoReleasePool if invoking 'autorelease' method more times. --- cocos2dx/cocoa/CCAutoreleasePool.cpp | 4 +--- cocos2dx/cocoa/CCObject.cpp | 11 +++++++---- cocos2dx/cocoa/CCObject.h | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/cocos2dx/cocoa/CCAutoreleasePool.cpp b/cocos2dx/cocoa/CCAutoreleasePool.cpp index d6583c296f..1e5143694c 100644 --- a/cocos2dx/cocoa/CCAutoreleasePool.cpp +++ b/cocos2dx/cocoa/CCAutoreleasePool.cpp @@ -41,8 +41,6 @@ CCAutoreleasePool::~CCAutoreleasePool(void) void CCAutoreleasePool::addObject(CCObject* pObject) { - CCAssert(false == pObject->m_bManaged, "this object is already in autorelese pool"); - m_pManagedObjectArray->addObject(pObject); CCAssert(pObject->m_uReference > 1, "reference count should be greater than 1"); @@ -70,7 +68,7 @@ void CCAutoreleasePool::clear() if(!pObj) break; - pObj->m_bManaged = false; + --(pObj->m_uAutoReleaseCount); //(*it)->release(); //delete (*it); #ifdef _DEBUG diff --git a/cocos2dx/cocoa/CCObject.cpp b/cocos2dx/cocoa/CCObject.cpp index 8084c65180..dc807406c7 100644 --- a/cocos2dx/cocoa/CCObject.cpp +++ b/cocos2dx/cocoa/CCObject.cpp @@ -38,7 +38,7 @@ CCObject* CCCopying::copyWithZone(CCZone *pZone) } CCObject::CCObject(void) -:m_bManaged(false) +:m_uAutoReleaseCount(0) ,m_uReference(1) // when the object is created, the reference count of it is 1 ,m_nLuaID(0) { @@ -51,9 +51,12 @@ CCObject::~CCObject(void) { // if the object is managed, we should remove it // from pool manager - if (m_bManaged) + if (m_uAutoReleaseCount > 0) { - CCPoolManager::sharedPoolManager()->removeObject(this); + for (int i = 0; i < m_uAutoReleaseCount; ++i) + { + CCPoolManager::sharedPoolManager()->removeObject(this); + } } // if the object is referenced by Lua engine, remove it @@ -98,7 +101,7 @@ CCObject* CCObject::autorelease(void) { CCPoolManager::sharedPoolManager()->addObject(this); - m_bManaged = true; + ++m_uAutoReleaseCount; return this; } diff --git a/cocos2dx/cocoa/CCObject.h b/cocos2dx/cocoa/CCObject.h index 2aff081ee1..010e55df62 100644 --- a/cocos2dx/cocoa/CCObject.h +++ b/cocos2dx/cocoa/CCObject.h @@ -55,8 +55,8 @@ public: protected: // count of references unsigned int m_uReference; - // is the object autoreleased - bool m_bManaged; + // count of autorelease + unsigned int m_uAutoReleaseCount; public: CCObject(void); virtual ~CCObject(void); From 6d29e632941ca04d5401bd4a2b05d82eaabf698b Mon Sep 17 00:00:00 2001 From: James Chen Date: Mon, 8 Oct 2012 14:05:03 +0800 Subject: [PATCH 2/2] fixed #1494: Change the value of CCObject::m_uAutoreleaseCount in CCAutoreleasePool class. --- cocos2dx/cocoa/CCAutoreleasePool.cpp | 7 +++++-- cocos2dx/cocoa/CCObject.cpp | 7 +------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/cocos2dx/cocoa/CCAutoreleasePool.cpp b/cocos2dx/cocoa/CCAutoreleasePool.cpp index 1e5143694c..791226942a 100644 --- a/cocos2dx/cocoa/CCAutoreleasePool.cpp +++ b/cocos2dx/cocoa/CCAutoreleasePool.cpp @@ -44,13 +44,16 @@ void CCAutoreleasePool::addObject(CCObject* pObject) m_pManagedObjectArray->addObject(pObject); CCAssert(pObject->m_uReference > 1, "reference count should be greater than 1"); - + ++(pObject->m_uAutoReleaseCount); pObject->release(); // no ref count, in this case autorelease pool added. } void CCAutoreleasePool::removeObject(CCObject* pObject) { - m_pManagedObjectArray->removeObject(pObject, false); + for (int i = 0; i < pObject->m_uAutoReleaseCount; ++i) + { + m_pManagedObjectArray->removeObject(pObject, false); + } } void CCAutoreleasePool::clear() diff --git a/cocos2dx/cocoa/CCObject.cpp b/cocos2dx/cocoa/CCObject.cpp index dc807406c7..ff8f6a5eba 100644 --- a/cocos2dx/cocoa/CCObject.cpp +++ b/cocos2dx/cocoa/CCObject.cpp @@ -53,10 +53,7 @@ CCObject::~CCObject(void) // from pool manager if (m_uAutoReleaseCount > 0) { - for (int i = 0; i < m_uAutoReleaseCount; ++i) - { - CCPoolManager::sharedPoolManager()->removeObject(this); - } + CCPoolManager::sharedPoolManager()->removeObject(this); } // if the object is referenced by Lua engine, remove it @@ -100,8 +97,6 @@ void CCObject::retain(void) CCObject* CCObject::autorelease(void) { CCPoolManager::sharedPoolManager()->addObject(this); - - ++m_uAutoReleaseCount; return this; }