内存泄漏:
问题描述:
分析是显示内存泄漏其中I在下面的代码段分配文件路径值FILEB:内存泄漏:
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)的前一个值还没有被释放。但我无法弄清楚如何阻止这种泄漏。
答
没有。filePath没有问题。问题几乎可以肯定与您的两个属性propertyA
和propertyB
。如果它们是保留属性,那么分配给它们的数组及其内容将会泄漏,因为您拥有您分配的数组,并且您没有释放它们。改变这样的行:
self.propertyA = [[[NSMutableArray alloc] initWithContentsOfFile:filePath] autorelease];
// ^^^^^^^^^^^ will release ownership of the array
有些事情你不告诉我们。 – 2012-07-05 15:37:13