First of all:
NSLog(string);
Do not do this. (I just realized that this comes directly from Apple. Weird documents.) The first argument to NSLog is the format string. If your string contains some percentage of shoots, bad things will happen. Correct if a slightly longer path:
NSLog(@"%@", string);
Now: a point with auto-implemented objects does not have a null value. They save the score 1+ and have a pending -1 operation on them, which will be "soon in the future".
The exact meaning of "soon in the future" depends on the situation. If youre on the main topic and there is no additional pool of autoresists in place, autorealized objects will be released at the next runloop iteration. This is not necessary if you have an additional release pool:
// Let's pretend this is a long loop and you don't want to wait // for the autoreleased objects to be collected by the main pool. for (…) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString *foo = [NSString stringWith…]; [pool drain]; // Now foo is no longer valid. }
As for returned objects with auto-realization, this is one of the main use cases for auto-implementations. You return an object that will soon die, but if the calling user is interested, he can save and take possession of the property. (It seems that if you forgive the image by dropping a bomb with a burning safety fuse. If the calling user is interested, hell extinguished the fuse, saving it.) And if the calling user is not interested, for example, perhaps ignoring the output from or simply using the value to construct any of another object, it does nothing, and the object exits memory:
- (id) createObject { return [NSString stringWith…]; } - (void) interestedCaller { NSString *value = [[self createObject] retain]; } - (void) notInterestedCaller { [self createObject];
It is really convenient and makes manual memory management quite enjoyable. You might be interested in the launch cycles and the Objective-C tutorial by Scott Stevenson .
zoul
source share