iDevGames Forums
Autorelease Pools - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: Autorelease Pools (/thread-1475.html)



Autorelease Pools - Talyn - Apr 10, 2009 11:49 PM

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?


Autorelease Pools - Skorche - Apr 11, 2009 07:34 AM

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.


Autorelease Pools - SethWillits - Apr 11, 2009 06:48 PM

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?


Autorelease Pools - Josh - Apr 11, 2009 07:21 PM

Autorelease pools aren't too tricky. Is there some part of them that's throwing you?