From 285b8ce76f6b13ff9ac62ad8e8a05e8ce843c955 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A2=E5=85=8B?= Date: Sun, 20 May 2012 00:45:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _posts/2012-05-18-life-before-arc.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/_posts/2012-05-18-life-before-arc.md b/_posts/2012-05-18-life-before-arc.md index 0702a4c..c93eac9 100644 --- a/_posts/2012-05-18-life-before-arc.md +++ b/_posts/2012-05-18-life-before-arc.md @@ -102,11 +102,8 @@ id obj = [[NSObject alloc] init]; ![Autorelase Pool](http://farm6.staticflickr.com/5332/7225706146_bec59b116a.jpg) 当对objc调用autorelease方法时候,便释放其所有权,此时当前活动的autorelease pool拥有此对象,当当前活动autorelease pool被销毁时,autorelease pool所拥有的所有对象会收到release消息. -AutoreleasePool不能调用autorelease方法,否则会报错 - -> *** Terminating app due to uncaught exception 'NSInvalidArgumentException' reason: '*** -[NSAutoreleasePool autorelease]: -Cannot autorelease an autorelease pool' - +AutoreleasePool不能调用autorelease方法,否则会报错. +
很多时候我们发现并不需要自己去创建Autorelease Pool,这是为什么呢? 因为在主线程中每一次RunLoop开始的时候会自动创建一个Autorelease Pool,结束的时候销毁这个Autorelease Pool ![Runloop Autorelease Pool](http://farm8.staticflickr.com/7233/7225706212_8c66a0cb80.jpg) @@ -115,8 +112,10 @@ AutoreleasePool是可以嵌套的,你可以想象成每创建一个NSAutorelease 有时候我们为了程序的性能考虑,需要自己在适当的地方加上autorelease pool,以便及时释放掉内存.比如下面这种情况 {% highlight objc %} -for (int i = 0; i < numberOfImages; ++i) {
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - //Loading images, etc.
 //Too many autoreleased objects exist. +for (int i = 0; i < numberOfImages; ++i) { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + //Loading images, etc. + //Too many autoreleased objects exist. [pool drain]; //All the autoreleased objects are released by [pool drain]. }