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