简体   繁体   English

为什么这个Objective-C代码会泄漏内存?

[英]Why is this Objective-C code leaking memory?

Why does this leak? 为什么泄漏?

The arrayOfPerformances is a NSMutableArray , (nonatomic, retain) property that is synthesized. arrayOfPerformances是一个NSMutableArray(nonatomic, retain)属性,它是合成的。

The currentPerformanceObject is a Performance * , (nonatomic, retain) property that is synthesized. currentPerformanceObject是一个合成的Performance *(nonatomic, retain)属性。

Performance is a custom class Performance是一个自定义类

if(self.arrayOfPerformances == nil)
    {
        self.arrayOfPerformances = [[NSMutableArray alloc]init];
    }

    [self.arrayOfPerformances addObject:currentPerformanceObject];
    [currentPerformanceObject release];
    currentPerformanceObject = nil;

You are creating a new array and retaining it at the same time in this line, because you are invoking a (retain) property setter with the dot notation: 您正在创建一个新数组在此行中同时保留它,因为您使用点表示法调用(retain)属性设置器:

// Your property
@property (nonatomic, retain) NSMutableArray *arrayOfPerformances;

// The offending code
self.arrayOfPerformances = [[NSMutableArray alloc]init];

Because of that, the locally-created array is leaking because you don't release it. 因此,本地创建的数组正在泄漏,因为您没有释放它。 You should autorelease that array, or create a temporary local var, assign, then release the local var, like so: 您应该自动释放该数组,或者创建一个临时的本地var,赋值,然后释放本地var,如下所示:

// 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];

If your .arrayOfPerformances property is never released (it would usually be released in -dealloc ), than the array itself, plus any object in the array will be leaked when this object is dealloced. 如果你的.arrayOfPerformances属性从未被释放(它通常会在-dealloc释放),那么除了数组本身之外,当释放此对象时,数组中的任何对象都将被泄露。

You need to release both properties in your -dealloc : 您需要在-dealloc释放这两个属性:

- (void)dealloc
{
    ... other deallocs
    self.arrayOfPerformances = nil;
    self.currentPerformanceObject = nil;
    [super dealloc];
}

Also, as @BoltClock has pointed out, you need to release or auto-release your NSMutableArray . 此外,正如@BoltClock指出的那样,您需要释放或自动释放您的NSMutableArray The best way to do this is to initialize it using the autoreleased method: 最好的方法是使用自动释放的方法初始化它:

self.arrayOfPerformances = [NSMutableArray array];

Also, you don't need to release your currentPerformanceObject , you should just set the property to nil, since setting the retain ed property to nil will release it for you. 此外,您不需要释放currentPerformanceObject ,您应该将该属性设置为nil,因为将retain ed属性设置为nil将为您释放它。 Your code should probably look something like this: 您的代码应该看起来像这样:

if (self.arrayOfPerformances == nil) {
    self.arrayOfPerformances = [NSMutableArray array];
}
[self.arrayOfPerformances addObject:self.currentPerformanceObject];
self.currentPerformanceObject = nil;

This line is the culprit: 这条线是罪魁祸首:

self.arrayOfPerformances = [[NSMutableArray alloc]init];

The retain count is 1 after the alloc/init. alloc / init之后的保留计数为1。 Setting the value via the arrayOfPerformances property setter increments the retain count again (because it's a retain property). 通过arrayOfPerformances属性setter设置值会再次增加保留计数(因为它是一个retain属性)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM