Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using retain and release for Objects

Tags:

cocos2d-x

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

like image 708
AndroidDev Avatar asked Jan 13 '14 07:01

AndroidDev


People also ask

What does retain mean in Objective-C?

You send an object a retain message when you want to prevent it from being deallocated until you have finished using it. An object is deallocated automatically when its reference count reaches 0 .

Which is used to automate memory management for Objective-c objects?

"Manual Retain-Release" or MRR In MRR, we explicitly manage memory by keeping track of objects on our own. This is implemented using a model, known as reference counting, that the Foundation class NSObject provides in conjunction with the runtime environment.

What is Autorelease Objective-C?

An autorelease pool is actually a collection of objects that will be released at some point in the future (either at the end of the thread's run loop or at the end of the scope of an autorelease pool). When a pool is drained, all the objects in the pool at that time are sent the release message.

How memory managed in Objective-C?

i said: Memory mangement are used the retain count way in which memory is managed in Objective-C. When we create a object it has retain count of 1. When we send an object a retain message, its retain count is incrementedby 1. And when we send release message retain count is decreased by 1.


2 Answers

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.

like image 154
Losiowaty Avatar answered Oct 24 '22 22:10

Losiowaty


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.

like image 20
Jain Avatar answered Oct 25 '22 00:10

Jain