【转】cocos2d-x初探学习笔记--内存回收机制

之前提到过cocos2d-x的内存回收机制,但是不是很全面,这篇主要写一下release和autorelease的区别,使用autorelease可以简化我们的内存管理,等于将我们的对象放入自动释放池,放入自动对象池的对象会记着你在晚些时候发送的释放信息,因为忘记释放内存有可能会导致内存泄露,当然这样做也有一个缺点,就是当下一帧我们需要再次使用这个对象的时候,我们就会发现我们的对象已经被释放了,就算你把对象作为成员变量,都无法保证不会出现这种错误,这时你有两种办法,第一种办法就是使用retain方法保留,第二种办法就是在addchild的时候,加上tag,然后再当我们需要的时候,我们就可以通过tag再次获得相应的对象即可。
虽然苹果开发者文档建议较少的使用自动释放对象,但是这样做可以方便我们进行内存管理。
和oc一样,cocos2d引擎对于内存管理有些约束,如果你拥有一些对象,你必须在用完之后释放他,如果你使用了autorelease你就不能使用release
使用自动释放的例子


使用释放的例子


使用保留的例子


刚开始研究此引擎,如有错误之处,希望大家多多指正
http://zhongyi.sina.com/zx/news/319037.shtml http://zhongyi.sina.com/zx/news/319035.shtml http://zhongyi.sina.com/zx/news/319034.shtml http://zhongyi.sina.com/zx/news/319033.shtml http://zhongyi.sina.com/zx/news/319030.shtml http://zhongyi.sina.com/zx/news/319029.shtml http://zhongyi.sina.com/zx/news/319028.shtml http://zhongyi.sina.com/zx/news/319027.shtml http://zhongyi.sina.com/zx/news/319026.shtml http://zhongyi.sina.com/zx/news/319025.shtml http://zhongyi.sina.com/zx/news/319023.shtml http://zhongyi.sina.com/zx/news/319021.shtml http://zhongyi.sina.com/zx/news/319020.shtml http://zhongyi.sina.com/zx/news/319018.shtml http://zhongyi.sina.com/zx/news/319017.shtml http://zhongyi.sina.com/zx/news/319015.shtml http://zhongyi.sina.com/zx/news/319014.shtml http://zhongyi.sina.com/zx/news/319013.shtml http://zhongyi.sina.com/zx/news/319012.shtml http://zhongyi.sina.com/zx/news/319011.shtml http://zhongyi.sina.com/zx/news/319010.shtml http://zhongyi.sina.com/zx/news/319009.shtml http://zhongyi.sina.com/zx/news/319008.shtml http://zhongyi.sina.com/zx/news/319007.shtml http://zhongyi.sina.com/zx/news/319006.shtml http://zhongyi.sina.com/zx/news/319004.shtml http://zhongyi.sina.com/zx/news/319003.shtml http://zhongyi.sina.com/zx/news/319002.shtml http://zhongyi.sina.com/zx/news/319001.shtml http://zhongyi.sina.com/zx/news/318999.shtml

楼主广告发得飞起,但是怎么这么多天了还没被抓到啊?:2::2::2: