2011-03-31 55 views
0

我正在处理简单的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值。但是我在这里的示例代码的第一部分发布它。

我在做什么错了?

+1

有你尝试了“产品 - >分析” – Seega 2011-03-31 09:10:24

回答

1

我不知道有关的泄漏,但是你在做“scoreTable释放”,但你的scoreTable已经在自动释放模式(你叫[NSMutableArray的arrayWithContentsOfFile:plistPath]),所以它不会保留

+0

它是,但它被添加到当前的自动发布池中......这是不同的...... – Macmade 2011-03-31 09:08:18

+2

好吧,也许我的措辞是不正确的 - 但想法是 - 这里有一个双重版本,这是一个禁忌。 – reflog 2011-03-31 09:09:46

+0

以任何方式双释放都不会分配内存。为什么我的内存泄漏总数达到+32字节? – 2011-03-31 09:13:51