Wednesday, May 2, 2012

Is this the right way to use NSAutoreleasePool?

I'm new to Objective-C and I'm not sure if I'm using NSAutoreleasePool the right way.




  1. If I want to use autorelease only one time I use:



    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSString *newText = [NSString stringWithFormat:@"%d", prograssAsInt];
    sliderLabel.text = newText;
    [pool release]; //newText will be released

  2. If I want to use autorelease several times I use:



    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSString *newText = [NSString stringWithFormat:@"%d", prograssAsInt];
    sliderLabel.text = newText;
    [pool drain]; //newText will be released
    newText = [NSString stringWithFormat:@"%d", prograssAsInt];
    sliderLabel.text = newText;
    [pool drain]; //newText will be released
    newText = [NSString stringWithFormat:@"%d", prograssAsInt];
    sliderLabel.text = newText;
    [pool release]; //newText will be released



Is this OK? Are there any memory leaks?





No comments:

Post a Comment