接上一节内容:cocos2dx - v2.3.3编辑器骨骼动画

本节主要Cocos2dx中节点的管理及应用

一般用法

  用过Cocos2dx应该都有用过addChild,removeChild方法。或者retain,release方法。

addChild,removeChild方法都是继承Node类,retain,release方法则继承自Ref类。

查看cocos2dx库代码可以看到,Ref中retain,release主要维持了一个_referenceCount计数变量

通过retain方法可以使_referenceCount计数加1,同时release可以使_referenceCount计数减1,并且在_referenceCount计数为0时,释放对象。

void Ref::retain()
{
CCASSERT(_referenceCount > , "reference count should be greater than 0");
++_referenceCount;
} void Ref::release()
{
CCASSERT(_referenceCount > , "reference count should be greater than 0");
--_referenceCount; if (_referenceCount == )
{
delete this;
}
}

查看Node的实现也可以了解Node中维护了一个CCVector对象来存储子节点,在erase子节点时,对子节点调用release方法,同时在pushBack新节点时,对子节点调用retain方法。

下面是CCVector中pushBack及erase的一些细节:

    // Adds objects

    /** Adds a new element at the end of the Vector. */
void pushBack(T object)
{
CCASSERT(object != nullptr, "The object should not be nullptr");
_data.push_back( object );
object->retain();
}
/** @brief Removes from the vector with an iterator.
* @param position Iterator pointing to a single element to be removed from the Vector.
* @return An iterator pointing to the new location of the element that followed the last element erased by the function call.
* This is the container end if the operation erased the last element in the sequence.
*/
iterator erase(iterator position)
{
CCASSERT(position >= _data.begin() && position < _data.end(), "Invalid position!");
(*position)->release();
return _data.erase(position);
}

这样就说明了Node节点实际还是在对Ref继承的_referenceCount进行计数处理。

若Node没有添加到父节点中,那么_referenceCount什么时候变成0,又什么时候释放节点的?

首先在Ref的构造函数中,可以看到_referenceCount初始化为1

Ref::Ref()
: _referenceCount() // when the Ref is created, the reference count of it is 1
{
#if CC_ENABLE_SCRIPT_BINDING
static unsigned int uObjectCount = ;
_luaID = ;
_ID = ++uObjectCount;
_scriptObject = nullptr;
#endif #if CC_REF_LEAK_DETECTION
trackRef(this);
#endif
}

同时,在Node的静态创建方法Node::create中,宏定义  CREATE_FUNC(Node)  实现如下,

/** @def CREATE_FUNC(__TYPE__)
* Define a create function for a specific type, such as Layer.
*
* @param __TYPE__ class type to add create(), such as Layer.
*/
#define CREATE_FUNC(__TYPE__) \
static __TYPE__* create() \
{ \
__TYPE__ *pRet = new(std::nothrow) __TYPE__(); \
if (pRet && pRet->init()) \
{ \
pRet->autorelease(); \
return pRet; \
} \
else \
{ \
delete pRet; \
pRet = nullptr; \
return nullptr; \
} \
}

可以看到,在对象创建失败会直接delete,并返回nullptr。创建成功则调用了autorelease方法,如下:

Ref* Ref::autorelease()
{
PoolManager::getInstance()->getCurrentPool()->addObject(this);
return this;
}

说明在正常创建Node节点时,会将其添加到coco2dx的一个内存池进行管理。同时在主循环中看到,下一帧处理对当前内存池对象进行了一次清理,并调用了release方法。

void DisplayLinkDirector::mainLoop()
{
if (_purgeDirectorInNextLoop)
{
_purgeDirectorInNextLoop = false;
purgeDirector();
}
else if (_restartDirectorInNextLoop)
{
_restartDirectorInNextLoop = false;
restartDirector();
}
else if (! _invalid)
{
drawScene(); // release the objects
PoolManager::getInstance()->getCurrentPool()->clear();
}
}
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
}

因此,如果一个对象在创建后,没有及时进行retain或者添加到父节点中,在当前帧结束时被调用release方法导致 _referenceCount变成0,从而被释放。

几个示例:

Scene* pScene = Director::getInstance()->getRunningScene();
if (pScene)
{
Node* pNode1 =Node::create(); // pNode1创建后没有处理,在当前帧结束前被释放 Node* pNode2 =Node::create(); // pNode2创建后添加到场景中,不会被释放
pScene->addChild(pNode2); Node* pNode3 =pScene->getChildByTag();
if(pNode3)
{
pScene->removeChild(pNode3); // pNode3被立即释放
pNode2->addChild(pNode3); // 错误!! pNode3已被释放
} Node* pNode4 =Node::create();
pNode2->addChild(pNode4); // pNode4 随 pNode2释放 Node* pNode5 =Node::create();
pScene->addChild(pNode5);
pNode5->retain();
pScene->removeChild(pNode5);
pNode2->addChild(pNode5);
pNode5->release(); // pNode5 随 pNode2释放 }

最新文章

  1. Go build constraints
  2. ExtJS4 源码解析(一)带项目分析
  3. Microsoft Azure 的负载平衡器的Session Sticky
  4. MySQL [Warning]: IP address &#39;xxxx&#39; could not be resolved: Name or service not known
  5. explicit constructor(显示构造函数)
  6. SQL Server 地理数据库中的系统表
  7. 在js传递参数中含加号(+)的处理方式
  8. 关于matlab矩阵卷积conv2和傅里叶变换求卷积ifft2的关系
  9. rpm 命令详解
  10. 【Python】 uuid生成唯一ID
  11. GPIO输入输出各种模式(推挽、开漏、准双向端口)详解
  12. 常用API3 BigData
  13. 基于netty实现的长连接,心跳机制及重连机制
  14. OC 中property的使用
  15. loadrunner&#160;脚本开发-文件下载
  16. POJ - 3267 The Cow Lexicon(动态规划)
  17. EC2(elastic compute cloud,弹性计算云,又称EC2实例)
  18. Fortran编译器之一GUN Fortran安装(Windows XP)
  19. 利用 share code 插件同步代码片段
  20. js写插件教程深入

热门文章

  1. ini文件必须要全路径名啊
  2. 201521123072《Java程序设计》第6周学习总结
  3. 201521123008《Java程序设计》第四周学习总结
  4. locale命令设置语言环境
  5. 201521123038 《Java程序设计》 第十二周学习总结
  6. 快递鸟顺丰物流api接口对接多种方法整理
  7. JavaSE(十)之Map总结
  8. 压缩空格的函数以及BCD码与ASCII相互转换函数
  9. oracle 建表空间-&gt;创建用户并把表空间分配给用户-&gt;给用户授权-&gt;导库
  10. 编程从入门到提高,然后放弃再跑路(Java)