CCObject deallocation


#1

Do I need to explicitly delete CCObjects like CCSprites, CCLabelBMFonts, and CCNodes, or are they automatically deallocated? I remember reading somewhere that their deallocation was handled automatically. Also, if I don’t need to deallocate CCSprites, does that mean I don’t have to deallocate objects that extend ccsprites?


#2

It’s in sticky FAQ > Memory deallocation ….


CCObjects will not be autoreleased except:
* you call object
>autorelease() manually
* you get the object from static methods such as CCSprite::spriteWithFile, CCLabelTTF::labelWithString, CCMenuItemImage::itemFromNormalImage, the static methods invoke autorelease() method in it.


#3

okay, but I have a `Ball` class that extends CCSprite. If I do:
Ball *b = new Ball();
will I need to call:
delete b;?


#4

Oh, no, please don’t “delete” CCObject*, use obj~~>release instead. Classes inherited from CCObject must use the CCAutoreleasePool mechanism.
In your code
<pre>
Ball *b = new Ball; // b~~>m_uReference = 1
layer~~>addChild; // b~~>m_uReference = 2
b~~>release; // b~~>m_uReference = 1

When layer is released
layer->release();  // b->m_uReference = 0, b is deleted in the engine internal.

#5

If you new an object, you should call release().

You can refer to this topic: http://www.cocos2d-x.org/boards/6/topics/678?r=1177#message-1177


#6

Ah okay. So if I use `new` to create an object, I should use `autorelease()` or `release()`. Even if `Ball` is a subclass of `CCSprite`, it will still be properly deleted. Because of the autorelease pool, I don’t have to worry too much about explicit garbage collection, unless I use `new`. Am I correct?


#7

yes, that is right