1
votes

I was reading the Memory Management Guide for IPhone OS and I didn' t understand a point in the Autorelease Pools section Listing - 1 code example:

void main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSArray *args = [[NSProcessInfo processInfo] arguments];

for (NSString *fileName in args) {

    NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];

    NSError *error = nil;
    NSString *fileContents = [NSString stringWithContentsOfFile:fileName
                                       encoding:NSUTF8StringEncoding error:&error];

    /* Process the string, creating and autoreleasing more objects. */

    [loopPool release];
}

/* Do whatever cleanup is needed. */
[pool drain];

exit (EXIT_SUCCESS);
} 

It says that :

".......Additionally, any autoreleased objects created in the context of the for loop (such as fileName) are released when loopPool is released even if they’re not explicitly sent an autorelease message."

The point that I didn't understand is how the fileName variable is included in the second pool(loopPool) but not the first one(pool). Isn't the fileName created when the first pool is the top most pool in the pool stack ?

1

1 Answers

1
votes

You are right. fineName is in the outer pool. If it's like this in the docs, it's a bug.

Edit: Feel free to file a bug report at Apple's radar system.