我的問題是關於+ 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;
}
加里