所以直接通过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; }}总结
以上是内存溢出为你收集整理的cocos2dx CCObject引用计数,内存释放分析(1)全部内容,希望文章能够帮你解决cocos2dx CCObject引用计数,内存释放分析(1)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)