大家好,
这些天我被困在一些内存泄漏上。我正在制作的应用程序是这样工作的:
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