1

Analyzeは、次のコードスニペットでfileBにfilePath値を割り当てるメモリリークを示しています。

NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];

NSString *filePath = [docsDir stringByAppendingPathComponent:@"/fileA"];

if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
    self.propertyA = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
} else {
    //  initialize array with default values and write to data file fileA   
    [self populatePropertyAForFile:filePath];       
}

filePath = [docsDir stringByAppendingPathComponent:@"/fileB"];

if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
    self.propertyB = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
} else {
    // initialize array with default values and write to data file fileB

    [self populatePropertyBForFile:filePath];

}

以前の値(fileAの場合)がリリースされていないためだと理解しています。しかし、私はこのリークを止める方法を理解することはできません。

4

1 に答える 1

2

いいえ。filePathに問題はありません。問題はほぼ確実に2つのプロパティにpropertyAありpropertyBます。それらがプロパティを保持している場合、割り当てた配列を所有していて、それらを解放していないため、それらに割り当てた配列とその内容がリークします。次のように行を変更します。

self.propertyA = [[[NSMutableArray alloc] initWithContentsOfFile:filePath] autorelease];
                                                                        // ^^^^^^^^^^^ will release ownership of the array
于 2012-07-05T15:37:24.883 に答える