Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c for循环中的自动释放池';语境_Objective C - Fatal编程技术网

Objective c for循环中的自动释放池';语境

Objective c for循环中的自动释放池';语境,objective-c,Objective C,我正在阅读IPhone操作系统的内存管理指南,但我不理解清单1“自动释放池”部分中的一点代码示例: void main() { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSArray *args = [[NSProcessInfo processInfo] arguments]; for (NSString *fileName in args) { NSAutoreleasePool *loopPool

我正在阅读IPhone操作系统的内存管理指南,但我不理解清单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);
} 
它说:

“……此外,在for循环上下文中创建的任何自动释放对象(如文件名)在释放loopPool时被释放,即使它们没有显式发送自动释放消息。”


我不明白的一点是,fileName变量如何包含在第二个池(loopPool)中,而不是第一个池(pool)。当第一个池是池堆栈中最顶层的池时,不是创建了文件名吗?

你说得对。fineName在外池中。如果文档中是这样的,那就是一个bug


编辑:请随意在苹果的雷达系统上提交错误报告。

你说得对。fineName在外池中。如果文档中是这样的,那就是一个bug


编辑:可以在苹果的雷达系统上随意提交一份bug报告。

你说得对,似乎应该改为
fileContents
!真的,请确保提交一个bug,这样其他人就不必自己面对这个问题(甚至相信这个!)。实际上,文件名可能不在任何自动释放池中。快速枚举要求集合构建要枚举的对象的C数组。因为它已经拥有这些对象的所有权,所以不需要在将它们放入数组时自动释放它们。由于processInfo对象是一个单例对象,因此它可能不在自动发布池中,并且它的属性(包括参数数组)可能都不在自动发布池中。您是对的,它应该是
fileContents
!真的,请确保提交一个bug,这样其他人就不必自己面对这个问题(甚至相信这个!)。实际上,文件名可能不在任何自动释放池中。快速枚举要求集合构建要枚举的对象的C数组。因为它已经拥有这些对象的所有权,所以不需要在将它们放入数组时自动释放它们。由于processInfo对象是一个单例对象,因此它可能不在自动发布池中,并且可能没有任何属性,包括arguments数组。