概述Cocos2dx 内存机制 CCDirector 主循环 每次清理内存池 void DisplayLinkDirector::mainLoop() { if (_purgeDirectorInNextLoop) { _purgeDirectorInNextLoop = false; purgeDirector(); } else if
Cocos2dx 内存机制 CCDirector 主循环 每次清理内存池 voID displaylinkDirector::mainLoop() { if (_purgeDirectorInNextLoop) { _purgeDirectorInNextLoop = false; purgeDirector(); } else if (! _invalID) { drawScene(); // release the objects PoolManager::getInstance()->getCurrentPool()->clear(); } } CCNode 继承自ccref,每次创建都会讲当前node加入自动回收池 Node * Node::create() { Node * ret = new (std::nothrow) Node(); if (ret && ret->init()) { ret->autorelease(); } else { CC_SAFE_DELETE(ret); } return ret; } 当每一个节点从渲染树销毁的时候都会调动~node()方法,将单签node从内存池中清理掉 Node::~Node() { cclOGINFO( "dealLocing Node: %p - tag: %i",this,_tag ); #if CC_ENABLE_SCRIPT_BINDING if (_updateScriptHandler) { ScriptEngineManager::getInstance()->getScriptEngine()->removeScriptHandler(_updateScriptHandler); } #endif // User object has to be released before others,since userObject may have a weak reference of this node // It may invoke `node->stopAllAction();` while `_actionManager` is null if the next line is after `CC_SAFE_RELEASE_NulL(_actionManager)`. CC_SAFE_RELEASE_NulL(_userObject); // attributes CC_SAFE_RELEASE_NulL(_glProgramState); for (auto& child : _children) { child->_parent = nullptr; } removeAllComponents(); CC_SAFE_DELETE(_componentContainer); #if CC_USE_PHYSICS setPhysicsBody(nullptr); #endif CC_SAFE_RELEASE_NulL(_actionManager); CC_SAFE_RELEASE_NulL(_scheduler); _eventdispatcher->removeEventListenersForTarget(this); #if CC_NODE_DEBUG_VERIFY_EVENT_ListENERS && COCOS2D_DEBUG > 0 _eventdispatcher->deBUGCheckNodeHasNoEventListenersOnDestruction(this); #endif CCASSERT(!_running,"Node still marked as running on node destruction! Was base class onExit() called in derived class onExit() implementations?"); CC_SAFE_RELEASE(_eventdispatcher); } 清空内存池 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 } CCRef 将当前对象加入内存池中 Ref* Ref::autorelease() { PoolManager::getInstance()->getCurrentPool()->addobject(this); return this; } 加入内存池具体的 *** 作 CCautoReleasePool voID autoreleasePool::addobject(Ref* object) { _managedobjectArray.push_back(object); } voID autoreleasePool::clear()
{
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
_isClearing = true;
#endif
std::vector<Ref*> releasings;
releasings.swap(_managedobjectArray);
for (const auto &obj : releasings)
{
obj->release();
}
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
_isClearing = false;
#endif
}
//回到CCDirector中进行垃圾回收 // release the objects PoolManager::getInstance()->getCurrentPool()->clear(); 总结
以上是内存溢出为你收集整理的cocos2dx 3.x内存管理全部内容,希望文章能够帮你解决cocos2dx 3.x内存管理所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
评论列表(0条)