【麦可网】Cocos2d-X跨平台游戏开发---学习笔记
第十二课:Cocos2D-X内存管理1-3
=======================================================================================================================================================================
课程目标:
-Cocos2D-X内存管理机制
课程重点:
-C\C++内存管理机制
-引用计数内存管理机制
-自动释放机制
考核目标
-理解引用计数内存管理机制
-合理运用自动释放机制
=======================================================================================================================================================================
一、C\C++内存管理机制
new/delete(会执行构造和析构函数) GameObject *gameObject = new GameObject(); gameObject->iCount = 0; Delete gameObject; GameObject *gameObject2 = new GameObject[5]; delete[] gameObject2;
---------------------------------------------------------------------------- malloc/free(不会执行构造函数和析构函数) GameObject *gameObject = (GameObject *)malloc(sizeof(GameObject)); gameObject->iCount = 0; free(gameObject);
二、引用计数内存管理机制
引用计数内存管理机制是为了解决以下情况的问题: GameObject *gameObjet = new GameObject; GameObject *gameObject2 = gameObject; gameObject->iFlag = 1; delete gameObject; gameObject2->iFlag = 0; //由于上一步中释放了内存,此步会报错误。 解决办法: GameObject *gameObjet = new GameObject; GameObject *gameObject2 = gameObject; gameObject->iCount += 1; gameObject->iFlag = 1; gameObject->myFree(); gameObject2->iFlag = 0; gameObject2->myFree(); 其中: GameObject::GameObject() { iCount = 1; } GameObject::~GameObject() { } void GameObject::myFree() { --iCount; if(iCount <= 0) { delete this; } }
三、Cocos2D-X里的引用计数机制
sprite->retain(); //引用计数加1 sprite->release(); //引用计数减1,判断是否释放资源 sprite->retaincount(); <span style="white-space:pre"> </span>//返回引用计数的值 set\get; CCObject::CCObject(void) //节点里的构造函数将引用计数设置为1 { static unsigned int uObjectCount = 0; m_uID = ++uObjectCount; m_nLuaID = 0; // 当对象创建时,引用计数设置为1 m_uReference = 1; m_bManaged = false; } 例: CCSprite *sprite = new CCSprite(); //引用计数=1 sprite->initWithFile(“HelloWorld.png”); CCSprite *sprite2 = sprite; sprite->retain(); <span style="white-space:pre"> </span>//引用计数=2 sprite->release(); <span style="white-space:pre"> </span> //引用计数=1 sprite2->release();
-------------------------------------------------- 例: CCSprite *sprite = new CCSprite(); //引用计数=1 sprite->initWithFile(“HelloWorld.png”); this->addChild(sprite); //引用计数=2 this->removeChild(sprite,true); //引用计数=1 sprite->release; //引用计数=?(被释放完了)
四、节点树里的引用计数
virtual void addChild(CCNode *child); virtual void addChild(CCNode *child,int zNode); virtual void addChild(CCNode *child,int zOrder,int tag); virtual void removeChild(CCNode *child,bool cleanup); void removechildByTag(int tag,bool cleanup); virtual void removeAllChildrenWithCleanup(bool cleanup);
添加的时候,引用计数会加1;
移除的时候,引用计数会减1。
===================================================================
总结:
引用技术内存管理机制,在保持new后紧跟delete的风格的同时,解决了资源正确释放。
开心一刻:
清明节快到了,某人去买祭品,看到居然有纸糊的苹果手机,有些不以为然:啊哈,烧苹果手机,老祖宗会用吗?店老板白了他一眼说:乔布斯都亲自下去教了,你还操那心?张三觉得有道理,便也买了一个。老板提醒:买个充电器吧!小心回来问你要,回来要是小事,叫你送过去就麻烦了!
【麦可网】Cocos2d-X跨平台游戏开发---下载地址:http://pan.baidu.com/s/1kTio1Av
【麦可网】Cocos2d-X跨平台游戏开发---笔记系列:http://blog.csdn.net/qiulanzhu