2010-11-27 22 views
0

这是一种常见情况:
在下面的代码中(简体),添加到数组的字典数量根据数据集的大小(如往常一样)而不同。
因此,不可能将每个字典与相应的“发布”声明进行匹配。
我认为释放数组(在dealloc中)会释放所有附加的字典?
尽管如此,它会产生泄漏。如何消除它?如何防止使用CorePlot使用的字典在阵列上发生泄漏

// -----myController.h---- 
@interface ExerciseGraphController : UIViewController <CPPlotDataSource>{   
    NSMutableArray   *plotDataForBar; 
} 
@property(readwrite, retain, nonatomic)NSMutableArray *plotDataForBar;     

// -----myController.m---- 
- ...getPlotData... { 

     //Solution 
     if (plotDataForBar){ 
      [plotDataForBar release]; 
     } 

     plotDataForBar = [[NSMutableArray array] init];        

     //for each item in plotDataForBar {      
      ...           //Populates plotDataForBar with dictionaries. 
      [plotDataForBar addObject: [NSDictionary dictionaryWithObjectsAndKeys:  // **LEAK** (on __NSCFDictionary, __NSArrayM, and _GeneralBlock16) 
       [NSDecimalNumber numberWithFloat:xF], [NSNumber numberWithInt:CPBarPlotFieldBarLocation],  
       [NSDecimalNumber numberWithFloat:yF], [NSNumber numberWithInt:CPBarPlotFieldBarLength], 
       nil]]; 
     }                
}              //Reads plotDataForBar in: 
- ...numberOfRecordsForPlot...{       //  Method required by CorePlot 
    return [plotDataForBar count];  
}   
- ...numberForPlot... {         //  "  "  "  " 
    ... 
    NSDecimalNumber *num = [[plotDataForBar objectAtIndex:index] objectForKey:[NSNumber numberWithInt:fieldEnum]]; 
    ...  
} 
- ...dealloc { 
    [plotDataForBar release]; 
    [super dealloc]; 
} 

回答

1

的代码量丢失,只是一种猜测:

你不应该在测试“...... getPlotData ......”如果该属性已被设置,或释放旧实例?每次调用此方法时都会发生泄漏。

+0

就是这样!我在上面的代码中添加了'解决方案'。非常感谢! – sambaMan 2010-11-27 22:26:44