皆さんこんにちは、
私は最近、いくつかのメモリリークで立ち往生しています。私が作っているアプリは次のように動作しています:
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:.. が漏れています。
autoreleased オブジェクトに関する Apple のドキュメントを読みました。そして、そのような新しいプールを再作成しようとしましたが、成功しませんでした:
-(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