从源代码版本号3.x。转载请注明
cocos2d-x 总的文件夹的源代码分析:
http://blog.csdn.net/u011225840/article/details/31743129
void Ref::retain() { CCASSERT(_referenceCount > 0, "reference count should greater than 0"); ++_referenceCount; }
Ref* Ref::autorelease() { PoolManager::getInstance()->getCurrentPool()->addObject(this); return this; }
void Ref::release() { CCASSERT(_referenceCount > 0, "reference count should greater than 0"); --_referenceCount; if (_referenceCount == 0) { delete this; } }
(AutoreleasePool的源代码一会再分析)
void AutoreleasePool::clear() { #if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0) _isClearing = true; #endif for (const auto &obj : _managedObjectArray) { obj->release(); } _managedObjectArray.clear(); #if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0) _isClearing = false; #endif }
void DisplayLinkDirector::mainLoop() { if (_purgeDirectorInNextLoop) { _purgeDirectorInNextLoop = false; purgeDirector(); } else if (! _invalid) { drawScene(); // release the objects PoolManager::getInstance()->getCurrentPool()->clear(); } }
PoolManager* PoolManager::getInstance() { if (s_singleInstance == nullptr) { 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); } return s_singleInstance; }
poolManager是单例模式,当第一次初始化的时候,会自己主动生成一个AutoreleasePool,并将其放入自己的stack中。可是。当你打开AutoreleasePool的构造函数时,发现当中已经有一个调用PoolManager::getInstance()->push(this); 通过debug跟踪。笔者发现此时有两个AutoRealsePool。即poolManager的stack内有两个位置都指向同一个AutoRealsePool。
感觉此处应该是一个Bug。
版权声明:本文博客原创文章,博客,未经同意,不得转载。