Autorelease Pools

Member
Posts: 215
Joined: 2008.06
Post: #1
Alright, I've had enough of wrestling with autorelease pools. I say: they aren't that helpful in the first place. They make a headache out of a relatively easy process (memory management) and cause more turmoil than convenience. Does anyone else feel this way? I believe that autorelease pools should be deprecated and phased out altogether. What am I missing that makes them so useful/important?

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
They give you the ability to return a value from a function that the function has not stored a reference to. The point of the autorelease pool is that you know the value will be freed sometime after your function returns, and sometime after the function that called the function that returned the autoreleased value returns. Without real garbage collection, strict retain/release can turn into a bit of a nightmare at times. Autorelease pools help make the rules of retaining and releasing simpler. There have been other threads about this in the past.

In a way, autorelease pools have been deprecated because Objective C does have garbage collection now. Just use that and smile that you don't have to worry about memory management anymore. Writing memory management code is not hard, but debugging it when there are problems can be very frustrating. Those are the times that garbage collection is your real friend.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
⌘-R in Chief
Posts: 1,258
Joined: 2002.05
Post: #3
Talyn Wrote:Alright, I've had enough of wrestling with autorelease pools. I say: they aren't that helpful in the first place. They make a headache out of a relatively easy process (memory management) and cause more turmoil than convenience. Does anyone else feel this way?

Absolutely not. If you think so, then you aren't using them properly.


Quote:What am I missing that makes them so useful/important?

If you've been doing Obj-C for any length of time, their purpose is pretty clear, but I'll make up an example...


With an autorelease pool:
Code:
NSOpenPanel * panel = [NSOpenPanel openPanel];

[panel setPrompt:@"Choose an image file…"];
[panel setAllowedTypes:[NSArray arrayWithObjects:@"jpg", @"gif", @"png", nil]];

if ([panel runModal] == NSOKButton) {
    NSMutableArray * relevantFiles = [NSMutableArray array];
    NSString * fullPath = [panel filename];
    NSString * parentPath = [fullPath stringByDeletingPathExtension];
    NSString * filename = [[fullPath lastPathComponent] stringByDeletingPathExtension];
    NSString * extension = [fullPath pathExtension];
    NSArray * siblingFiles = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:parentPath error:nil] sortedArrayUsingSelector:@selector(compare:)];
    
    for (NSString * siblingFilename in siblingFiles) {
        if ([[siblingFilename stringByDeletingPathExtension] rangeOfString:filename options:NSCaseInsensitiveSearch].location == 0) {
            [relevantFiles addObject:fullPath];
        }
    }
    
    for (NSString * file in relevantFiles) {
        NSLog(@"Found: %@", file);
    }
}




Without an autorelease pool (IOW, if they didn't exist and @"string" must be manually released):

Code:
NSOpenPanel * panel = [[NSOpenPanel alloc] init];
NSString * prompt = @"Choose an image file…";
NSArray * allowedTypes = [[NSArray alloc] initWithObjects:@"jpg", @"gif", @"png", nil];

[panel setPrompt:prompt];
[panel setAllowedTypes:allowedTypes];

for (NSString * type in allowedTypes) {
    [type release];
}

[prompt release];
[allowedTypes release];



if ([panel runModal] == NSOKButton) {
    NSMutableArray * relevantFiles = [[NSMutableArray alloc] init];
    NSString * fullPath = [panel filename];
    NSString * parentPath = [fullPath stringByDeletingPathExtension];
    NSString * filename = [[fullPath lastPathComponent] stringByDeletingPathExtension];
    NSString * extension = [fullPath pathExtension];
    NSArray * siblingFiles = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:parentPath error:nil] sortedArrayUsingSelector:@selector(compare:)];
    
    for (NSString * siblingFilename in siblingFiles) {
        NSString * siblingExtension = [siblingFilename pathExtension];
        
        if ([extension isEqual:siblingExtension]) {
            NSString * siblingBasename = [siblingFilename stringByDeletingPathExtension];
            
            if ([siblingBasename rangeOfString:filename options:NSCaseInsensitiveSearch].location == 0) {
                [relevantFiles addObject:siblingFilename];
            }
            
            [siblingBasename release];
        }
        
        [siblingExtension release];
    }
    
    
    for (NSString * file in relevantFiles) {
        NSLog(@"Found: %@", file);
    }
    
    
    for (NSString * file in relevantFiles) {
        [file release];
    }
    
    [relevantFiles release];
    [fullPath release];
    [parentPath release];
    [filename release];
    [extension release];
    [siblingFiles release];
}

[panel release];


The difference is obvious.


Why do you have so much trouble with them? Could you provide an example of where they've caused you so much headache and turmoil?
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #4
Autorelease pools aren't too tricky. Is there some part of them that's throwing you?
Quote this message in a reply
Post Reply