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

3

12

I'm reading around the web and the documentation but to be honest, I don't get it. Since I'm new to cocos2d-x I would like to understand better how the objects are created/retained and what I'm supposed to do to release them (if required). The thing that confuses me is the usage of smart pointers that I don't know very well.

Imagine that in my CCLayer (added to the CCScene) I add a CCSprite, so i do:

this->sprite = CCSprite::create("mySprite.png");
this->addChild(sprite);

then since I've used create() I'm supposed to release it somewhere? in the destructor of the CCLayer maybe? or I have nothing to do about that?

I know the basics of C++ so if I do "new" an object I actually have to delete it in the destructor or when I don't need it anymore, but what about the cocos2dx objects?

Girish answered 17/11, 2013 at 22:14 Comment(0)
B
22

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]

Beardless answered 29/11, 2013 at 7:41 Comment(1)
Simplest and most complete answer on this subject I could find.Hagioscope
A
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;
Azide answered 18/11, 2013 at 3:37 Comment(0)
H
0

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.

Hew answered 17/11, 2013 at 22:57 Comment(1)
when it reaches 1 it's released I think when reaches 0 not 1. yeah ?Kissner

© 2022 - 2024 — McMap. All rights reserved.