【发布时间】:2009-11-16 10:52:10
【问题描述】:
我的问题是关于在 +planet 中创建的对象的范围。我被告知“自动释放的对象将在它们创建的方法/函数的持续时间内一直存在” 在我的示例中,我假设行星实例的范围在 main() 和不在我执行初始 alloc/init 的方法内?
+(Planet *) planet {
gPlanetCount++;
return [[[self alloc] init] autorelease];
}
int main(int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Planet *outerMost;
outerMost = [Planet planet];
...
... some code
...
[pool drain];
return 0;
}
EDIT_001
int main(int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Planet *outerMost;
outerMost = [[Planet planet] retain]; // Added retain
...
... some code
...
[outerMost release]; // Added release
[pool drain];
return 0;
}
加里
【问题讨论】:
标签: objective-c cocoa