cocosd-x 内存管理

//cocos2d-x ,因为是从cocos2d-iphone 移植过来的, 不是东西借鉴了iphone上面的东西, 比如内存管理 , 是模仿iphone的内存管理机制

 //它是依靠retainCount 值来管理内存,当retainCount等于0时,内存会被真真的delete掉。当然要让cocos2d-x 来管理 对象 ,你所写的每一个对象必须继承 于ccobject .


1.new 出来的对象 ,初始retainCount 为1 , 这个对象 默认是由你 手动来释放的 (调用ccobject 的release , 此时retainCount 为0 ,cocos2d-x会delete 掉这个对象)。

 2.如果 调用 了autorelease(),对象 就完全由cocos2d-x来管理,你不需要去手动release ,它会在某一时刻来自动release.


》需要注意的一点:


1. 当对象没有调用autorelease 时,retainCount 还为1, 那么需要你手动release.否则内存溢出。

2. 对象如果 调用 了autorelease ,但是后面你如果还想用到这个对象你需要 手工retain一下。 retain 会导致retainCount 加1 ,所以retain后,如果 你不想用了记得release掉。



Father * father = new Father;
	father->autorelease();

	CCArray *arry = CCArray::array();

	debugInfo( _T("arr retain count : %d  \n")  , arry->retainCount()  );

	debugInfo( _T("father retain count : %d\n")  , father->retainCount());
	arry->addObject(father);
	debugInfo( _T("father retain count : %d\n")  , father->retainCount());
	arry->removeObject(father);
	debugInfo(  _T("father retain count : %d\n")  , father->retainCount());

	arry->release();

你可能感兴趣的:(delete,iPhone)