1
我讀了內存管理指南IPhone OS和我不'噸瞭解自動釋放池部分清單中的一個點 - 1級的代碼示例:自動釋放池進行循環的情況下
void main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *args = [[NSProcessInfo processInfo] arguments];
for (NSString *fileName in args) {
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
NSError *error = nil;
NSString *fileContents = [NSString stringWithContentsOfFile:fileName
encoding:NSUTF8StringEncoding error:&error];
/* Process the string, creating and autoreleasing more objects. */
[loopPool release];
}
/* Do whatever cleanup is needed. */
[pool drain];
exit (EXIT_SUCCESS);
}
它說那麼:
「.......另外,即使未明確發送autorelease消息,也會在釋放loopPool時釋放在for循環上下文中創建的任何自動釋放對象(如fileName)。 「
我不明白的是fileName變量如何包含在第二個池(loopPool)中,但不是第一個(池)。當第一個池是池堆棧中最靠前的池時,是不是創建了fileName?
你說得對,應該是'fileContents'!真的,請確保提交錯誤,以便其他人不必自己面對這個問題(或者甚至相信這一點!)。 – bddckr 2010-07-04 21:49:52
實際上,fileName可能不在任何autorelease池中。快速枚舉要求集合構建要列舉的對象的C數組。由於它已經擁有這些對象的所有權,因此它不需要自動釋放它們,因爲它將它們放入數組中。由於processInfo對象是單例,因此它可能不在自動發佈池中,並且可能沒有其屬性,包括參數數組。 – JeremyP 2010-07-05 10:36:11