Selon l'analyse d'Instruments, j'ai une fuite de mémoire ici - et - je ne sais pas comment libérer correctement l'objet scoresArray que j'ai créé.Libération d'un objet tableau
Ce code fonctionne correctement, à l'exception de la fuite. Je libère l'objet highScoresArray plus tard dans le code - mais tente de libérer le scoreArray tuer l'application. Je pensais que lorsque je publierais highScoresArray, je publierais scoresArray, car ils pointent tous les deux vers le même emplacement en mémoire. Si quelqu'un peut indiquer où ma pensée est imparfaite, ce serait génial.
- (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
}
}
}
Oui - highScoresArray est une variable d'instance - j'ai éliminé le scorehorsArray - merci pour l'entrée. – ReachWest