Cocos2d-x 内存管理机制
Cocos2d-x 内存管理机制
1. 引用计数Cocos2d-x 所有对象几乎都继承自Ref基类,该基类主要进行引用计数管理。 public:
void retain();
void release();
Ref* autorelease();
unsigned int getReferenceCount() const;
protected:
Ref();
protected:
/// count of references
unsigned int _referenceCount;
friend class AutoreleasePool;
当一个对象由 在仅有引用计数的情况下管理UI元素的示例: auto node = new Node(); // 引用计数为1
addChild(node); // 引用计数为2
// .......
node->removeFromParent(); // 引用计数为1
node->release(); // 引用计数为0,对象被删除
此时,如果忘记调用 2. 用autorelease()方法声明一个“智能指针”Cocos2d-x 使用 Ref* Ref::autorelease()
{
PoolManager::getInstance()->getCurrentPool()->addObject(this);
return this;
}
在以上方法中,Cocos2d-x通过 void DisplayLinkDirector::mainLoop()
{
if(! _invalid)
{
drawScene();
// release the object
PoolManager::getInstance()->getCurrentPool()->clear();
}
}
void AutoreleasePool::clear()
{
for(const auto &obj : _manageedObjectArray)
{
obj->release();
}
_manageObjectArray.clear();
}
auto node = new Node(); // 引用计数为1
node->autorelease(); // 加入智能指针池
该帧结束的时候 auto node = new Node(); // 引用计数为1
node->autorelease(); // 加入智能指针池
addChild(node); // 引用计数为2
该帧结束后 此时该 Cocos2d-x 使用静态的 Node* Node::create()
{
Node *ret = new Node();
if (ret && ret->init())
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
3. 参考资料
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- c# – 通过Lambda表达式分配值?
- 正则表达式自己整理
- react-native – TextInput中的按钮作为本机响应
- c# – 在向用户显示后立即删除动态生成的PDF文件
- 122.View the Exhibit and note the files available in th
- C++ 简单实现MFC ListControl 点击列头排序
- ruby-on-rails – 我可以在数组上使用update_all吗?
- jenkins执行构建任务报错之java.lang.NoSuchFieldError: DE
- xStream xml转java bean,抛出CannotResolveClassException
- 最接近2的力量