http://blog.csdn.net/dboylx/archive/2009/02/15/3893770.aspx
自动释放进阶,让我们更深一步了解它的工作原理
虽然自动释放对象的概念是如此简单,但了解它更多的工作原理还是很有必要的。不然在我们的嵌入式设备的开发中,仍然会走入内存漏洞深渊。
其实,在我们的一个应用中,是有很多的”NSAutoreleasePool”对象实例的,就像它的命名一样,它们用来收集所有自动释放的对象。只要在调用”autorelease”方法后,它就会被加入到这个池中。在未来的某个时刻,一般指在”Foundation”与”AppKit”应用一个事件循环结束时,或者在响应完”WebObjects”类应用请求时,或调用”NSAutoreleasePool”对象的”release”方法。这里需要注意,”NSAutoreleasePool”并不止一个,为什么需要多个”NSAutoreleasePool”来管理内存呢?因为,在一个代码段内就回收所有自动释放对象是很有用处的,多线程应用中,每个线程可以拥有一个自动释放池的栈,当你创建了一堆临时对象时,而仅仅是在一段很短的上下文中,比如一个简单循环,你并不希望在下面的代码中,他们仍然占用保宝贵的内存资源,你就可以为这段短小紧凑本地上下文创建一个”NSAutoreleasePool”对象来管理他们:
- (id) findSomething
{
id theObject = nil;
// Whatever we're looking for
NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];
// Autoreleased objects are now automatically placed in localPool.
// Loop that creates many temporary objects
while ( theObject == nil )
{
...
if ( [temporaryObject matchesSomeCondition] )
{
theObject = [temporaryObject retain];
// We want this one
}
}
// Get rid of all those temporary objects
[localPool release];
return [theObject autorelease];
}
上段代码我们做了什么:
A,我们创建了一个”NSAutoreleasePool”对象,把它压到当前上下文中的内存管理池顶,在它下面的所有自动内存管理对象都被放入到这个池中。
B,我们调用了自动释放对象”temporaryObject”的”retain”方法,使它的生命周期超过本地池的管理。
C,释放池操作,同时把它从栈中POP出去。
D,紧接着,我们又在返回它前调用了 ”autorelease”方法,把这个对象放入当前池栈的TOP池中。
这东西有点绕,但原理其实还算简单。如果上面的能明白了,恭喜你,Objective-C的水平又上了一个台阶。
同理可证,还有一段更精练的代码:
- (NSArray *) findAListOfThings
{
NSMutableArray *thingArray =
[[NSMutableArray alloc] initWithCapacity: 25];
// The list of 25 things we're looking for
NSAutoreleasePool *outerPool = [[NSAutoreleasePool alloc] init];
NSAutoreleasePool *innerPool = nil;
NSArray *largeObjectArray = nil;
id temporaryObject = nil;
NSEnumerator *arrayEnumerator = nil;
// Loops that create many temporary objects
while ( [thingArray count] != 25 )
{
largeObjectArray = [self fetchLotsOfObjects];
// largeObjectArray is autoreleased and contained in the
// outer autorelease pool
arrayEnumerator = [largeObjectArray objectEnumerator];
// Note that the enumerator itself is a temporary object!
// It will be released by the outerPool
// Create the inner pool on each iteration. When
// a pool is created, it automatically becomes the
// "top" pool on the current thread's stack of pools.
innerPool = [[NSAutoreleasePool alloc] init];
// autoreleased objects now go into innerPool
while ( temporaryObject = [arrayEnumerator nextObject] )
{
...
if ( [temporaryObject matchesSomeCondition] )
{
[thingArray addObject: temporaryObject];
// Collections retain their members
}
}
// Dispose temporary objects created on this iteration;
// Note that the objects added to thingArray during this
// iteration are also in innerPool and thus sent a release
// message, but are not destroyed because they have been
// retained by thingArray and so have an additional reference
// (their retainCount > 1)
[innerPool release];
}
[outerPool release];
return [thingArray autorelease];
}

518

被折叠的 条评论
为什么被折叠?



