为什么会泄漏?arrayOfPerformances
是合成的NSMutableArray
,(nonatomic, retain)
属性。currentPerformanceObject
是合成的Performance *
,(nonatomic, retain)
属性。Performance
是自定义类
if(self.arrayOfPerformances == nil)
{
self.arrayOfPerformances = [[NSMutableArray alloc]init];
}
[self.arrayOfPerformances addObject:currentPerformanceObject];
[currentPerformanceObject release];
currentPerformanceObject = nil;
最佳答案
您正在创建一个新的数组并在该行中同时保留它,因为您正在调用带有点标记的(retain)
属性设置器:
// Your property
@property (nonatomic, retain) NSMutableArray *arrayOfPerformances;
// The offending code
self.arrayOfPerformances = [[NSMutableArray alloc]init];
因此,本地创建的数组正在泄漏,因为您没有释放它。您应该自动释放该数组,或者创建一个临时本地变量,赋值,然后释放本地变量,如下所示:
// Either this
self.arrayOfPerformances = [[[NSMutableArray alloc] init] autorelease];
// Or this (props Nick Forge, does the same as above)
self.arrayOfPerformances = [NSMutableArray array];
// Or this
NSMutableArray *newArray = [[NSMutableArray alloc] init];
self.arrayOfPerformances = newArray;
[newArray release];