Ganzolo
Ganzolo

Reputation: 1394

Autorelease methods leaking

Hi everyone,

I'm stuck these days on some memory leaks. The app I'm making is working like that :

1 - Loads a file into memory
2 - Create a screen according to some values read on that file
3 - Display the view

Far from now everything is normal when I start the app and get the first screen. There is no leaks.

But when I want to load an other screen from the current view I got plenty of leaks from autoreleased objects. And I don't understand because when I load a new view from the current one the process is similar :

1 - Desctruction of the current view
2 - Loads a file into memory
3 - Create a screen according to some values read on that file
4 - Display the view

Here are some concrete example of what is leaking :

-(NSString*)pathForApplicationName:(NSString*)appName
                         withImage:(NSString*)imagePath { 
       NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES);
       NSString *documentPath = [searchPaths lastObject];
       return [NSString stringWithFormat:@"%@/%@/assets/www/%@",documentPath,[self convertSpacesInDashes:appName],imagePath];
 }

The stringWithFormat:.. is leaking. An other example :

-(UIColor*)convertHexColorToUIColor:(NSString*)hexColor {

    if ([hexColor length] == 7) {
        unsigned c = 0;
        NSScanner *scanner = [NSScanner scannerWithString:hexColor];
        [scanner setScanLocation:1];
        [scanner scanHexInt:&c];


        return [UIColor colorWithRed:((c>>16)&0xFF)/255.0 
                               green:((c>>8)&0xFF)/255.0 
                                blue:(©&0xFF)/255.0 
                               alpha:1.];
    }
    else {
        return nil;
    }
}

Same, colorWithRed:.. is leaking.

I've read apple's documentation regarding autoreleased objects. And I've even tried to recreate a new pool like that, without any success :

-(UIView)myFonctionWhoGenerateScreens:

    for () {

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

        // There are all of the autoreleased method calls that are leaking...

        [subPool drain];

    }    
}

I think I am missing something. Does anyone has an idea?

Leak backtrace enter image description here

Thanks a lot.

Edit :

Here is how the return of the leaking function is handled in applyPropertyName:withPropertyType:onComponent:withValue:forStyling method :

else if ([propertyType isEqualToString:@"AB_IMAGE"]) {

        UIImage *image = [UIImage imageWithContentsOfFile:[self pathForApplicationName:applicationName
                                                                             withImage:value]];
        @try {
            [component setValue:image
                         forKey:propertyName];
        }
        @catch (NSException *exception) {
#if DEBUG_CONTROLLER
            NSLog(@" %@ Not key-value compliant for <%@,%@>",component,propertyName,image);
#endif
        }
    }

Edit 2 : Here is the complete back trace http://ganzolo.free.fr/leak/%20Leak.zip

Upvotes: 2

Views: 740

Answers (1)

Phillip Mills
Phillip Mills

Reputation: 31016

Looking at your Leak document, and picking the [NSString stringWithFormat] object at address 0xdec95c0 as an example, it shows balanced retain count operations for the Foundation, ImageIO, and CoreGraphics use of the object but ABTwoImageItemImageLeftComponent is still holding an unreleased reference.

0   0xdec95c0   CFString (immutable)    Malloc  1   00:39.994.343   144 Foundation  +[NSString stringWithFormat:]
1   0xdec95c0   CFString (immutable)    Autorelease <null>  00:39.994.376   0   Foundation  +[NSString stringWithFormat:]
2   0xdec95c0   CFString (immutable)    CFRetain    2   00:39.994.397   0   iOS Preview App -[ABTwoImageItemImageLeftComponent setSrcProperty:]
3   0xdec95c0   CFString (immutable)    CFRetain    3   00:39.996.231   0   ImageIO CGImageReadCreateWithFile
4   0xdec95c0   CFString (immutable)    CFRetain    4   00:39.998.012   0   CoreGraphics    CGPropertiesSetProperty
5   0xdec95c0   CFString (immutable)    CFRelease   3   00:40.362.865   0   Foundation  -[NSAutoreleasePool release]
6   0xdec95c0   CFString (immutable)    CFRelease   2   01:14.892.330   0   CoreGraphics    CGPropertiesRelease
7   0xdec95c0   CFString (immutable)    CFRelease   1   01:14.892.921   0   ImageIO releaseInfoJPEG

Upvotes: 1

Related Questions