NSArray中的内存泄漏,长时间跟踪

时间:2012-04-10 15:29:31

标签: ios

以下self.listOfCustDetail和self.listOfCustomer

中存在内存泄漏
-(void) calCustList {
    NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString  *documentsDirectory = [paths objectAtIndex:0];  
    NSString  *plistPath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"customer.plist"];

    self.listOfCustDetail = [[[NSMutableArray alloc] init] autorelease];
    self.listOfCustomer = [[[NSMutableArray alloc] init] autorelease];
    self.customers =  [[[NSMutableDictionary alloc] initWithContentsOfFile:plistPath] autorelease];

    [self.listOfCustomer removeAllObjects];
    [self.listOfCustDetail removeAllObjects];

    [self.listOfCustomer addObject:@"新紀錄"];
    [self.listOfCustDetail addObject:@""];

    for (id key in self.customers) {
        NSString *s = [NSString stringWithFormat:@"%@,%@,%@,%@", [[self.customers objectForKey:key] objectAtIndex:0], [[self.customers objectForKey:key] objectAtIndex:1], [[self.customers objectForKey:key] objectAtIndex:2], [[self.customers objectForKey:key] objectAtIndex:3]];
        [self.listOfCustomer addObject:key];
        [self.listOfCustDetail addObject:s];
    }  
}

2 个答案:

答案 0 :(得分:0)

您的房产如何定义?他们保留了吗?如果他们确实保留了,那么您正在展示的代码中没有实际错误。

该方法多久被调用一次?如果经常调用它,您可以使用自定义NSAutoreleasePool

在新的初始化数组中也不需要以下两行:

[self.listOfCustomer removeAllObjects];
[self.listOfCustDetail removeAllObjects];

您是否正在显示实际的源代码?

答案 1 :(得分:0)

我想建议,在Project中启用ARC(自动引用计数),因为您不需要释放数组