Objective c NSString stringByAppendingPathComponent上的内存泄漏:

Objective c NSString stringByAppendingPathComponent上的内存泄漏:,objective-c,ios,Objective C,Ios,Analyze显示内存泄漏,我在下面的代码段中为fileB分配filePath值: NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0]; NSString *filePath = [docsDir stringByAppendingPathComponent:@"/fileA"]; if ([[NSFileMan

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)尚未发布。但我想不出怎么堵住这个漏洞

否。文件路径没有问题。问题几乎肯定出在您的两个属性上,
propertyA
propertyB
。如果它们是保留属性,那么您分配给它们的数组及其内容将泄漏,因为您拥有您分配的数组,并且您没有释放它们。按如下方式更改行:

self.propertyA = [[[NSMutableArray alloc] initWithContentsOfFile:filePath] autorelease];
                                                                        // ^^^^^^^^^^^ will release ownership of the array

有些事你没告诉我们,嗯。。是的,我没有放自动释放的错。谢谢你指出这一点。另外,Xcode并不指向内存泄漏的位置,而是指向已分配泄漏内存的对象的位置。