【发布时间】:2012-02-06 07:31:01
【问题描述】:
对不起,我是 cocoa 编程的新手,我不确定我是否真的了解 nsautoreleasepool 的工作原理。
我读到的每一个地方都说 NSAutoreleasePool 负责所有自动释放调用(谈论最后一个 NSAutoreleasePool 声明)。
考虑以下代码:
int main(int argc, char *argv[]) {
//NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
//[pool release];
return retVal;
}
在我的应用程序的某些时候,我也会有类似的东西:
NSString* b = [[NSString alloc] initWithFormat:@"%d", 10];
[b autorelease];
考虑到我在任何地方都没有NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];...[b autorelease]; 不应该使应用程序崩溃吗?但该应用程序似乎运行良好。
观察:我不打算在没有 NSAutoreleasePool 的情况下编写应用程序,我只是想弄清楚它是如何工作的。这个事实让我怀疑我所知道的。
【问题讨论】:
标签: iphone cocoa-touch autorelease nsautoreleasepool