释放数组对象

时间:2010-02-23 21:04:23

标签: objective-c memory-management nsmutablearray

根据Instruments分析,我在这里有内存泄漏 - 而且 - 我不确定如何正确释放我创建的scoresArray对象。

除了泄漏之外,此代码确实可以正常工作。我稍后在代码中释放了highScoresArray对象 - 但尝试释放scoresArray会杀死应用程序。我以为当我发布highScoresArray时,我会释放scoresArray,因为它们都指向内存中的相同位置。如果有人能够指出我的想法存在缺陷,那就太棒了。

- (void) readScoresFile {
    // Read the Scores File, if it exists
    NSString *filePath = [self scoresFilePath];
    // Only load the file if it exists at the path
    if ([[NSFileManager defaultManager] fileExistsAtPath: filePath]) {
        scoresFileExistsFlag = YES;
        NSLog(@"SCORES FILE EXISTS - THEREFORE LOAD IT");
        NSMutableArray *scoresArray = [[NSMutableArray alloc] initWithContentsOfFile: filePath];
        highScoresArray = scoresArray;
    } else {
        scoresFileExistsFlag = NO;
        NSMutableArray *scoresArray = [[NSMutableArray alloc] init];
        highScoresArray = scoresArray;

        // No Scores File exists - we need to create and save an empty one.
        int counter = 1;
        while (counter <= 5) {
            // Set up a date object and format same for inclusion in the Scores file
            NSDate *now = [[NSDate alloc] init];
            NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
            [dateFormat setDateFormat:@"yyyy.MM.dd"]; 
            NSString *theDateNow = [dateFormat stringFromDate:now];
            // Add the score data (Score and User and date) to the runScoreDataDictionary
            runScoreDataDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
                [NSNumber numberWithInt:0], @"score", 
                [NSNumber numberWithInt:0], @"landings",
                currentUser, @"user", 
                theDateNow, @"date", nil];
            //NSLog(@"Dictionary contains: %@", runScoreDataDictionary);
            // Add the dictionary to the highScoreArray
            [highScoresArray addObject:runScoreDataDictionary];
            //NSLog(@"OBJECTS in ARRAY: %i", [highScoresArray count]);

            [self writeScoresFile]; // Write the empty scores file to disk

            [now release];
            [dateFormat release]; 

            ++counter;

            //[scoresArray release]; // TESTING TO SEE IF THIS KILLS - YES KILLS
        }
    }
}

2 个答案:

答案 0 :(得分:1)

  

我稍后在代码中释放了highScoresArray对象 - 但尝试释放scoresArray会杀死应用程序。我以为当我发布highScoresArray时,我会释放scoresArray,因为它们都指向内存中的相同位置

只要你没有改变 highScoresArray指针指向另一个对象,释放它将与释放scoresArray相同。

NSMutableArray* highScoresArray;
NSMutableArray* scoresArray = [[NSMutableArray alloc] init];
highScoresArray = scoresArray;
[highScoresArray release]; // same as `[scoresArray release];`

但是如果你之后改变其中任何一个指向另一个对象,释放它们将不相同:

NSMutableArray* highScoresArray;
NSMutableArray* scoresArray = [[NSMutableArray alloc] init];
highScoresArray = scoresArray;
// ... Now make `highScoresArray` point to another object ...
highScoresArray = [[NSMutableArray alloc] init];
// Now you should release both as they point to different objects.
[highScoresArray release];
[scoresArray release];

当然,简单地调用addObject不会改变指针。它会更改指向的对象。只有将指针重新指定给另一个对象才是重要的。

答案 1 :(得分:1)

我猜highScoresArray是一个实例变量(因为它没有在你列出的方法中的任何地方声明)。这意味着在创建scoresArray(与highScoresArray相同的对象)时,它的保留计数为1.您没有retain它,所以release它会将其保留计数减少到0并且它将被清理 - 对于实例变量来说不是一件好事。

我也不确定你为什么要这样做:

NSMutableArray *scoresArray = [[NSMutableArray alloc] init];
highScoresArray = scoresArray;

您似乎无需在其他任何地方使用scoresArray,因此您可以这样做:

[highScoresArray release];    // Release the old object
highScoresArray = [[NSMutableArray alloc] init];