NSMutableArray上的内存泄漏

时间:2014-04-01 16:53:02

标签: ios7 ios6

我正在使用仪器工具来识别和修复内存泄漏。我不确定为什么仪器在NSMutableArray中显示泄漏:" PriceArr"和" KSKPrice"对象在下面的代码中:

NSArray *pricesJson =  [jsonDict valueForKeyConvertingNSNullToNil:@"prices"];
    **NSMutableArray *pricesArr = [[[NSMutableArray alloc]init]autorelease];**
    for (NSDictionary *priceJson in pricesJson) {
        **KSKPrice *price = [[KSKPrice alloc] initWithJSON:priceJson];**
        [pricesArr addObject:price];
        [price release];

    }
    self.prices = pricesArr;

我也尝试过NSMutableArray * pricesArr = [NSMutableArray array]但是仪器总是显示泄漏。

这是我正在正确释放数组的dealloc方法:

 (void)dealloc {
    [_image release];
    [_identifier release];
    [_calories release];
    [_prices release];
    [_name release];
    [super dealloc];
}

2 个答案:

答案 0 :(得分:0)

我想问的第一个显而易见的事情是,你为什么不使用ARC?只是想知道你是否对它的能力有任何疑问,或者你是否有技术问题转换。 ARC肯定会为你解决这个问题。

我要提的第二件事......我不明白你的for循环是什么。为什么你不能做这样的事情。

// I'm not sure, but i think this should just automatically add the dicts to a mutable array
NSMutableArray * pricesArray = (NSMutableArray *)[jsonDict valueForKeyConvertingNSNullToNil:@"prices"];


// If that doesn't work, this is another better solution
NSArray * pricesJson =  [jsonDict valueForKeyConvertingNSNullToNil:@"prices"];
NSMutableArray * pricesArray = [[NSMutableArray alloc] initWithArray:pricesJson];

答案 1 :(得分:0)

确保“self.prices”属性的生命周期正确无误。假设你的属性被设置为保留对象,而不是释放它 - (void)dealloc会导致可变数组和它的内容泄漏。你没有在这里发布这些代码,所以我无法确定。