Using retain and release for Objects
Asked Answered
I

2

5

Are there any general guide lines for using retain and release for objects in cocos2d-X ? When creating objects in a function, is it true that the functions memory is cleaned up the second the function returns. When a object is created, calling the retain function of the object, will retain object beyond the function return ?

Kind Regards

Intuitivism answered 13/1, 2014 at 7:37 Comment(0)
E
22

Generally in c++ you have this behaviour:

void foo() {
    Object a;
    Object *pA = new Object();
    (…)
 }

This would result in a being destroyed automatically at function end, as it was allocated on stack. The *pA would not get destroyed, as it was allocated on the heap (thus, you only loose the reference to it, but the object itself still lives).

Cocos implements a thing called "Automatic Reference Counting" : each CCObject has a reference counter and two methods retain() and release(). The way this works is, that every time you create an object, it gets registered in cocos structers (CCPoolManager). Then with every frame (between them being drawn) there is a maintenance loop which checks the reference counter of all objects : if it is 0 this means (to cocos) that no other objects reference it, so it is safe to delete it. The retain count of an object is automatically incresead when you use this object as an argument for an addChild function.

Example :

void cocosFoo() {
    CCSprite *a = CCSprite::create(…);
    CCSprite *b = CCSprite::create(…);

    this->addChild(b);
}

What happens here is this :

  1. Two CCSprites are created, cocos knows about them.
  2. The b sprite is added to this object (say a CCLayer)
  3. The function ends, no objects are destroyed (both of them being on heap).
  4. Somewhere between this and next frame, the maintanance gets run. Cocos chcecks both sprites and sees that a has reference count == 0, so it deletes it.

This system is quite good, as you don't need to worry about memory management. If you want to create a CCSprite (for example), but not add it as a child yet, you can call retain() on it, which will raise its reference counter, saving it from automatic deletion. But then you'd have to remember about calling release() on it (for example, when adding it as a child).

The general things you have to remeber about are :

  1. Each call to retain() by you needs to be paired with release().
  2. You generally shouldn't delete CCObjects yourself. If you feel that you need to, there is a conveniece macro : CC_SAFE_DELETE(object)

So to answer your questions in short :

  1. Are there any general guide lines for using retain and release for objects in cocos2d-X ?

    Yes, you should generally not need to do it.

  2. When creating objects in a function, is it true that the functions memory is cleaned up the second the function returns.

    Answer to this is the whole text above.

  3. When a object is created, calling the retain function of the object, will retain object beyond the function return ?

    Yes, as will adding it as a child to another (retained in any way) object.

Escamilla answered 13/1, 2014 at 10:32 Comment(2)
The most clear explanation I have seen on this subject (first time I read about the maintenance loop!). Thank you.Rutherford
Thanks for clearing that up. Perfect , concise and to the pointCurtsy
M
2

Here is the thing,

cocos2dx has an autorelease pool which drains the objects which have retain count=0 which is a variable to keep in check the scope of the cocos2dx object.

Now when you create new object using the create method it is already added to the autorelease pool and you don't need to release it or delete it anywhere , its like garbage collector in java, takes care of garbage objects behind your back.

But when you create new object using 'new' you definitely need to release it in its destructor or after its use is over.

Second thing,

when your object is added to the autorelease pool but you need it somewhere else you could just retain it , this increments its retain count by one and then you have to manually release it 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.

Maccabees answered 14/1, 2014 at 5:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.