我正在处理简单的iPhone游戏。当游戏结束时,我试图将一些分数信息保存到.plist文件。你能帮我理解我做错了什么吗?NSMutableArray内存泄漏问题
这里是 “完成” 的游戏方法:
- (void) gameDone {
// something ....
// save our current score values
NSDictionary *currentScore = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:[score getScoreValue]], @"score", [NSNumber numberWithFloat:[time getSeconds]], @"time", nil];
[score saveValueToFile:currentScore toFile:fileName];
[currentScore release];
// something ....
}
这里是2种方法从我的其他类保存一个分数:
- (NSMutableArray *) loadFromFile:(NSString *) fileName {
{
// get a paths
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", fileName]];
NSMutableArray *scoreTable = [NSMutableArray arrayWithContentsOfFile:plistPath];
return scoreTable;
}
// save a score table to plist file
- (void) saveValueToFile:(NSDictionary *)value toFile:(NSString *)fileName {
// load all score table
NSMutableArray *scoreTable;
scoreTable = [self loadFromFile:fileName];
// insert new value
// [MEMORY_LEAKS_LINE] ATTENTION: Instrument > Memory Leaks point to this line
[scoreTable insertObject:value atIndex:[scoreTable count]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", fileName]];
[scoreTable writeToFile:path atomically:YES];
[scoreTable release];
}
当我启动新游戏(实际上我的游戏设计有可能连续玩许多游戏)内存泄漏仪器总是添加32个字节到总泄漏字节并链接到[MEMORY_LEAKS_LINE]
我认为我的问题可能与currentScore值。但是我在这里的示例代码的第一部分发布它。
我在做什么错了?
有你尝试了“产品 - >分析” – Seega 2011-03-31 09:10:24