Memory Management - Why do we need to retain()

Memory Management - Why do we need to retain()
0.0 0


After reading a couple chapters from the cocos2d-x book and a few articles and the documentation. I still do not understand what retain() , and cc_safe_release and autorelease() do and why we need them?

Is it a scope thing? Where when you leave a function the objects go out of scope?

In what specific senario or senario’s do we need to retain()?

Thanks in advance.


Because everything in Cocos is a CCObject, which manages memory for you by reference counting.

if in the next cycle, a CCObject is unused, it will be released.

retain() tells the autoreleasepool that you plan to use it later

a typical scenario is when you create some sprites that you want to add to a layer later, if you don’t retain them, they will be released, and become unusable the next update cycle