Cocos2dx memory management, how to use destructors and when to release objects?

孤街醉人 提交于 2019-11-29 06:06:43

Here is the thing,

Object of the class Ref or any class derived from it has a variable _retainCount which represents the scope of the object.

Also there is an autorelease pool which is similar to the garbage collector in java. The object which is added to this autorelease pool will be deleted at the end of the frame. Unless its _retainCount!=0

Now when you create new object of Ref or derived class using the create method it is already added to the autorelease pool and you don't need to release it or delete it anywhere. As you can see in the create function of Node below.

Node * Node::create()
{
    Node * ret = new (std::nothrow) Node();
    if (ret && ret->init())
    {
        ret->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(ret);
    }
    return ret;
}

But when you create new object using 'new' you definitely need to delete it after its use is over. Though it is not recommended to use New to allocate the objects of cocos2d classes. Rather use create.

Node* temp=Node::create();

then,

temp->retain();

//your work...

temp->release();

or

Node* temp=Node::create();
Node* tempChild=Node::create();
temp->addChild(tempChild);
//your work...
temp->removeFromParent();

Second thing,

when your object is added to the autorelease pool but you need to increase its scope you could just retain it , this increments its retain count by one and then you have to manually release it i.e, decrement its retain count after its use is over.

Third Thing,

Whenever you add child your object it is retained automatically but you don't need to release it rather you remove it from the parent as mentioned above.

Official documentation is @link below.

[http://www.cocos2d-x.org/wiki/Reference_Count_and_AutoReleasePool_in_Cocos2d-x#Refrelease-retain-and-autorelease][1]

Sorry,my English is poor!

1.The this->sprite will be auto release when CCLayer destroy if you call CCSprite::create(...); and addChild(...); .

2.if you want remove this->sprite sometimes ,you could call this:

this->sprite->removeFormParents();
this->sprite=NULL;

Cocos2d-x uses objective-c-like memory management system, so that variables have retain count. With each cocos main-loop pass objects' retain count decrements and when it reaches 1 it's released. CCObject containing another objects keep them alive by incrementing their retain count. So that CCObjects retains themselves and usually you don't need to worry about memory management. However, there are situations in witch you'd want to turn autorelease off and manually manage the CCObjects' lifetime with retain() and release(). There are some common situations with cocos2d-x leading to memory leaks such as having a CCObject contacting another CCObject that keep the first one alive using setUserObject(). Thus, both will never be destroyed.

So, in general, in most cases it is the best to stick with built-in autorealse model and manually managing some edge cases, that'd normally lead to memory leaks. Also, keep in mind that there are opposite situations - f.e. accessing a dead ccobject can lead to runtime error.

Offtopic: you don't have to write this when accessing member/method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!