This is a follow on from another question, my question is regarding the use of the retain/release in main(). From my understanding in this simple example the retain/release statements are not needed. BUT in more complex situations its best practice to add them as it ensures that the planet instance is not released unexpectedly.
+(Planet *) planet {
gPlanetCount++;
//return [[[Planet alloc] init] autorelease];
return [[[self alloc] init] autorelease]; // Better
}
int main(int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Planet *outerMost;
outerMost = [[Planet planet] retain];
...
... some code
...
[outerMost release];
[pool drain];
return 0;
}
EDIT_001
So I could better write the above as.
int main(int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Planet *outerMost;
outerMost = [Planet planet];
...
... some code
...
[pool drain];
return 0;
}
cheers gary