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
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 .
"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.
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.
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.
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 :
b
sprite is added to this
object (say a CCLayer)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 :
retain()
by you needs to be paired with release()
.CC_SAFE_DELETE(object)
So to answer your questions in short :
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With