cocos2d-x游戏开发(六)自动释放池

前端之家收集整理的这篇文章主要介绍了cocos2d-x游戏开发(六)自动释放池前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

欢迎转载:http://blog.csdn.net/fylz1125/article/details/8519887


上一篇文章写了引用计数和自动释放的大概流程,其中略过了自动释放池,这里简单述说。

前面提到CCObject的autorelease函数

  1. CCObject*CCObject::autorelease(void)
  2. {
  3. CCPoolManager::sharedPoolManager()->addObject(this);
  4. returnthis;
  5. }

这里的sharedPoolManager()函数表明CCPoolManager是个单例类。

题外话:cocos2d-x里面大量的用到了单例模式,每个单例类都有个明显的标志,就是它的实例获取函数,从命名规则上看都是sharedxxx()形式。

跟进这个addObject函数

?
    voidCCPoolManager::addObject(CCObject*pObject)
  1. getCurReleasePool()->addObject(pObject);
  2. }

这里就到了CCPoolManager,来看下这个类

?
    classCC_DLLCCPoolManager
  1. CCArray*m_pReleasePoolStack;
  2. CCAutoreleasePool*m_pCurReleasePool;
  3. CCAutoreleasePool*getCurReleasePool();
  4. public:
  5. CCPoolManager();
  6. ~CCPoolManager();
  7. voidfinalize();
  8. voidpush();
  9. voidpop();
  10. voidremoveObject(CCObject*pObject);
  11. voidaddObject(CCObject*pObject);
  12. staticCCPoolManager*sharedPoolManager();
  13. staticvoidpurgePoolManager();
  14. friendclassCCAutoreleasePool;
  15. };
这里可以看到它维护一个自动释放池CCAutoreleasePool,还有个CCArray变量,是个栈结构,来管理自动释放池。

看下实现:

?
    #include"CCAutoreleasePool.h"
  1. #include"ccMacros.h"
  2. NS_CC_BEGIN
  3. //自动释放池管理员实例
  4. staticCCPoolManager*s_pPoolManager=NULL;
  5. CCAutoreleasePool::CCAutoreleasePool(void)
  6. {
  7. m_pManagedObjectArray=newCCArray();//自动释放的对象放在这个Array里面
  8. m_pManagedObjectArray->init();
  9. }
  10. CCAutoreleasePool::~CCAutoreleasePool(void)
  11. CC_SAFE_DELETE(m_pManagedObjectArray);
  12. }
  13. voidCCAutoreleasePool::addObject(CCObject*pObject)
  14. m_pManagedObjectArray->addObject(pObject);//实质上的添加函数,加到对象管理数组里面
  15. CCAssert(pObject->m_uReference>1,"referencecountshouldbegreaterthan1");//断言
  16. ++(pObject->m_uAutoReleaseCount);
  17. pObject->release();//norefcount,inthiscaseautoreleasepooladded.
  18. voidCCAutoreleasePool::removeObject(CCObject*pObject)
  19. for(unsignedinti=0;i<pObject->m_uAutoReleaseCount;++i)
  20. m_pManagedObjectArray->removeObject(pObject,false);
  21. voidCCAutoreleasePool::clear()
  22. if(m_pManagedObjectArray->count()>0)
  23. //CCAutoreleasePool*pReleasePool;
  24. #ifdef_DEBUG
  25. intnIndex=m_pManagedObjectArray->count()-1;
  26. #endif
  27. CCObject*pObj=NULL;
  28. CCARRAY_FOREACH_REVERSE(m_pManagedObjectArray,pObj)
  29. if(!pObj)
  30. break;
  31. --(pObj->m_uAutoReleaseCount);
  32. //(*it)->release();
  33. //delete(*it);
  34. #ifdef_DEBUG
  35. nIndex--;
  36. #endif
  37. m_pManagedObjectArray->removeAllObjects();
  38. //--------------------------------------------------------------------
  39. //
  40. //CCPoolManager
  41. CCPoolManager*CCPoolManager::sharedPoolManager()//获取管理员实例
  42. if(s_pPoolManager==NULL)
  43. s_pPoolManager=newCCPoolManager();//如果没有实例,调构造函数
  44. returns_pPoolManager;
  45. voidCCPoolManager::purgePoolManager()
  46. CC_SAFE_DELETE(s_pPoolManager);
  47. CCPoolManager::CCPoolManager()
  48. m_pReleasePoolStack=newCCArray();//栈结构,管理自动释放池
  49. m_pReleasePoolStack->init();
  50. m_pCurReleasePool=0;
  51. CCPoolManager::~CCPoolManager()
  52. finalize();
  53. //weonlyreleasethelastautoreleasepoolhere
  54. m_pCurReleasePool=0;
  55. m_pReleasePoolStack->removeObjectAtIndex(0);
  56. CC_SAFE_DELETE(m_pReleasePoolStack);
  57. voidCCPoolManager::finalize()
  58. if(m_pReleasePoolStack->count()>0)
  59. //CCAutoreleasePool*pReleasePool;
  60. CCObject*pObj=NULL;
  61. CCARRAY_FOREACH(m_pReleasePoolStack,pObj)//遍历对象池栈,清空所有对象池
  62. if(!pObj)
  63. break;
  64. CCAutoreleasePool*pPool=(CCAutoreleasePool*)pObj;
  65. pPool->clear();
  66. voidCCPoolManager::push()//管理员的push方法,将对象池栈压栈。当前对象池为NULL时才调用push函数
  67. CCAutoreleasePool*pPool=newCCAutoreleasePool();//ref=1
  68. m_pCurReleasePool=pPool;
  69. m_pReleasePoolStack->addObject(pPool);//ref=2
  70. pPool->release();//ref=1
  71. voidCCPoolManager::pop()//管理员的pop方法,将对象池栈弹栈,其实就是释放对象
  72. if(!m_pCurReleasePool)
  73. return;
  74. intnCount=m_pReleasePoolStack->count();
  75. m_pCurReleasePool->clear();
  76. if(nCount>1)
  77. m_pReleasePoolStack->removeObjectAtIndex(nCount-1);
  78. //if(nCount>1)
  79. //{
  80. //m_pCurReleasePool=m_pReleasePoolStack->objectAtIndex(nCount-2);
  81. //return;
  82. //}
  83. m_pCurReleasePool=(CCAutoreleasePool*)m_pReleasePoolStack->objectAtIndex(nCount-2);
  84. /*m_pCurReleasePool=NULL;*/
  85. voidCCPoolManager::removeObject(CCObject*pObject)
  86. CCAssert(m_pCurReleasePool,"currentautoreleasepoolshouldnotbenull");
  87. m_pCurReleasePool->removeObject(pObject);
  88. voidCCPoolManager::addObject(CCObject*pObject)
  89. getCurReleasePool()->addObject(pObject);//将对象添加到当前的对象池
  90. CCAutoreleasePool*CCPoolManager::getCurReleasePool()//获取当前对象池,
  91. if(!m_pCurReleasePool)
  92. push();//对象池由一个栈维护。当前对象池为空,则push一下,push函数会new一个对象池,然后添加到栈里
  93. returnm_pCurReleasePool;
  94. NS_CC_END

好了,就这些。

一个对象添加自动释放池后在什么时机被释放,这里不写了。后面有机会再写。

原文链接:https://www.f2er.com/cocos2dx/342236.html

猜你在找的Cocos2d-x相关文章