0

下面 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];
    }  
}
4

2 回答 2

0

您的属性是如何定义的?他们保留吗?如果他们确实保留了,那么您所显示的代码段中就没有实际错误。

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

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

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

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

于 2012-04-10T15:48:29.540 回答
0

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

于 2013-02-28T12:06:45.137 回答