anatoliy_v
anatoliy_v

Reputation: 1800

NSMutableArray memory leaks problems

I'm working on simple iPhone game. When a game finish i'm trying to save some scores info to a .plist file. Can you help me to understand what i'm doing wrong?

Here is method for a "Done" game:

- (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 ....

}

Here is 2 methods to save a scores from my other class:

- (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];   
}

When I launch a new game (actually my game designed with possibility to play many games one after another) Memory Leaks Instrument always add 32 bytes to Total Leaks Bytes and link to [MEMORY_LEAKS_LINE]

I thought my problem might be with currentScore value. But I release it in first part of sample code here.

What I'm doing wrong?

Upvotes: 0

Views: 380

Answers (1)

reflog
reflog

Reputation: 7645

i'm not sure about the leak, but you are doing 'scoreTable release' , but your scoreTable is already in autorelease mode (you called [NSMutableArray arrayWithContentsOfFile:plistPath]), so it's not retained

Upvotes: 1

Related Questions