gpt4 book ai didi

objective-c - 自动释放方法泄漏

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:50:41 26 4
gpt4 key购买 nike

大家好,

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

1 - 将文件载入内存
2 - 根据在该文件上读取的一些值创建一个屏幕
3 - 显示 View

从现在开始,当我启动应用程序并获得第一个屏幕时一切正常。没有泄漏。

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

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

这里有一些泄漏的具体例子:

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

}
}

我想我错过了什么。有人有想法吗?

泄漏回溯 enter image description here

非常感谢。

编辑:

下面是在 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

最佳答案

查看您的 Leak 文档,并以地址 0xdec95c0 处的 [NSString stringWithFormat] 对象为例,它显示了 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

关于objective-c - 自动释放方法泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11083182/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com