CCObject deallocation


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?


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.


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


Oh, no, please don’t “delete” CCObject*, use obj~~>release instead. Classes inherited from CCObject must use the CCAutoreleasePool mechanism.
In your code
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.


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

You can refer to this topic:


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?


yes, that is right