cocos2dx - ActionManager内存泄露
2015-10-16 16:32
507 查看
ActionManager memory leak
cocos2d-x3.7都3.7了还有这样的bug,真是好难过,不过还是好开源的,谁都可以贡献一下
问题描述:
当创建一个node,并让它run一个action的时,如果这个node没有添加到任何其它节点(即没有父节点),那么改node和action将一直不会被释放。原因:
在node调用runAction时,会将动画交给ActionManager来管理,即调用
ActionManager::addAction,看该函数的实现:
void ActionManager::addAction(Action *action, Node *target, bool paused) { //... if (! element) { element = (tHashElement*)calloc(sizeof(*element), 1); element->paused = paused; target->retain(); //!!!watch out!!! element->target = target; HASH_ADD_PTR(_targets, target, element); } //...
注意这里会把node给retain一次,那它是在哪里release的呢,看下面:
void ActionManager::update(float dt) { //... elt = (tHashElement*)(elt->hh.next); if (_currentTargetSalvaged && _currentTarget->actions->num == 0) { deleteHashElement(_currentTarget); } //... } void ActionManager::deleteHashElement(tHashElement *element) { ccArrayFree(element->actions); HASH_DEL(_targets, element); element->target->release(); // release node free(element); }
也就是说,当一个node在运行一个action时会被retain(referenceCount+1),在action运行完成后就会被release(referenceCount-1)。这么看起来没什么问题,但是当这个node不在running状态时,即没有添加到任何节点,通过create创建下一帧应该被自动释放,如果这时候给它运行了一个动画(
ActionManager::addAction时第三个参数paused为true),再看update函数:
void ActionManager::update(float dt) { if (! _currentTarget->paused) //动画不会被执行 { //... } //... elt = (tHashElement*)(elt->hh.next); if (_currentTargetSalvaged && _currentTarget->actions->num == 0) { deleteHashElement(_currentTarget); } //... }
此时动画一直不会执行,也就是说动画不会完成,那么
actions->num不可能等于0,则该节点就一直不会被释放。
解决方法:
void ActionManager::update(float dt) { //... elt = (tHashElement*)(elt->hh.next); if (_currentTargetSalvaged && _currentTarget->actions->num == 0) { deleteHashElement(_currentTarget); } //if some node refrence 'target', it's refrence count >= 2 (issues #14050) else if (_currentTarget->target->getReferenceCount() == 1) { deleteHashElement(_currentTarget); } //... }
那么问题来了:为什么会有这样的行为呢——创建一个node,不加到任何节点,还给它run一个action?
这里解释过一遍了:https://github.com/cocos2d/cocos2d-x/pull/14090
相关文章推荐
- cocos2dx常用宏
- cocos2d之Box2D详细说明 鼠标联合实现
- Cocos2d-x3.2 使用物理引擎进行碰撞检测[转]
- 【复习笔记】 cocos2d-x 2.x 渲染特效实现 一 基础回顾
- Cocos2D-X2.2.3学习笔记12(瞬间动作)
- cocos2dx 3.2 定义自己使用rapidjson阅读json数据
- Cocos2dx 对android支持原理
- Cocos2d中update与fixedUpdate的区别(六)
- Cocos2d中update与fixedUpdate的区别(六)
- Cocos2d中update与fixedUpdate的区别(六)
- Cocos2d中update与fixedUpdate的区别(五)
- Cocos2d中update与fixedUpdate的区别(五)
- Cocos2d中update与fixedUpdate的区别(五)
- cocos2dx3.0 超级马里奥开发笔记(两)——正确的规划游戏逻辑
- Cocos2d中update与fixedUpdate的区别(四)
- Cocos2d中update与fixedUpdate的区别(四)
- Cocos2d中update与fixedUpdate的区别(四)
- Cocos2d中update与fixedUpdate的区别(三)
- Cocos2d中update与fixedUpdate的区别(三)
- Cocos2d中update与fixedUpdate的区别(三)