2010-02-23 109 views
0

根據儀器分析,我在這裏有內存泄漏 - 並且 - 我不確定如何正確釋放我創建的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 
     } 
    } 
} 

回答

1

我猜highScoresArray是一個實例變量(因爲它沒有在你列出的方法中的任何地方聲明)。這意味着在創建scoresArray(與highScoresArray相同的對象)時,其保留計數爲1.您不需要retain它,因此它會將其保留計數遞減到0並清除它 - 對於一個實例變量來說不是一件好事。

我也不知道爲什麼你這樣做:

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

你似乎並不需要使用scoresArray其他地方,所以你可能只是這樣做:

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

是的 - highScoresArray是一個實例變量 - 我已經消除了多餘的scoresArray - 感謝您的輸入。 – ReachWest 2010-02-23 21:44:31

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不會改變指針 。它改變了指向的對象。只有將指針重新分配給另一個對象纔是重要的。

+0

非常感謝很多爲你的幫助。 – ReachWest 2010-02-23 21:48:26