2

大家好,

这些天我被困在一些内存泄漏上。我正在制作的应用程序是这样工作的:

1 - 将文件加载到内存中
2 - 根据在该文件上读取的一些值创建屏幕
3 - 显示视图

当我启动应用程序并进入第一个屏幕时,现在一切正常。没有泄漏。

但是当我想从当前视图加载另一个屏幕时,我从自动释放的对象中得到了很多泄漏。而且我不明白,因为当我从当前视图加载新视图时,过程是相似的:

1 - 当前视图的描述
2 - 将文件加载到内存中
3 - 根据在该文件上读取的一些值创建一个屏幕
4 - 显示视图

以下是一些泄漏的具体示例:

-(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];
 }

stringWithFormat:.. 正在泄漏。另一个例子 :

-(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;
    }
}

同样,colorWithRed:.. 正在泄漏。

我已经阅读了苹果关于自动释放对象的文档。而且我什至尝试过重新创建一个这样的新池,但没有任何成功:

-(UIView)myFonctionWhoGenerateScreens:

    for () {

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

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

        [subPool drain];

    }    
}

我想我错过了一些东西。有人有想法吗?

泄漏回溯在此处输入图像描述

非常感谢。

编辑 :

以下是在 applyPropertyName:withPropertyType:onComponent:withValue:forStyling 方法中如何处理泄漏函数的返回:

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
        }
    }

编辑 2:这是完整的回溯http://ganzolo.free.fr/leak/%20Leak.zip

4

1 回答 1

1

查看您的 Leak 文档,并选择[NSString stringWithFormat]地址 0xdec95c0 处的对象作为示例,它显示了该对象的 Foundation、ImageIO 和 CoreGraphics 使用的平衡保留计数操作,但ABTwoImageItemImageLeftComponent仍持有未发布的引用。

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
于 2012-06-18T13:36:00.303 回答