所以直接通过new创建的对象是没有加入自动释放池的。
CCObject::CCObject(void) : m_nLuaID(0),m_uReference(1) // when the object is created,the reference count of it is 1,m_uAutoReleaseCount(0) { static unsigned int uObjectCount = 0; m_uID = ++uObjectCount; }
CCObject* CCObject::autorelease(void) { CCPoolManager::sharedPoolManager()->addObject(this); return this; }
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. }
的对象引用计数减1,如果引用计数为0,就释放:
void CCAutoreleasePool::clear() { if(m_pManagedObjectArray->count() > 0) { //CCAutoreleasePool* pReleasePool; #ifdef _DEBUG int nIndex = m_pManagedObjectArray->count() - 1; #endif CCObject* pObj = NULL; CCARRAY_FOREACH_REVERSE(m_pManagedObjectArray,pObj) { if(!pObj) break; --(pObj->m_uAutoReleaseCount); //(*it)->release(); //delete (*it); #ifdef _DEBUG nIndex--; #endif } m_pManagedObjectArray->removeAllObjects(); } }
4、
在m_pManagedObjectArray->removeAllObjects()(在CCArray中)调用CCObject::release函数,
把引用计数减1,为0时,则delete对象,释放整个对象。
void CCObject::release(void) { CCAssert(m_uReference > 0,"reference count should greater than 0"); --m_uReference; if (m_uReference == 0) { delete this; } }原文链接:https://www.f2er.com/cocos2dx/343842.html