views:

29

answers:

1

Hi

My iPhone app is crashing and getting EXC_BAD_ACCESS error when using this code:

NSError *error;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
        NSString *documentsDirectory = [paths objectAtIndex:0]; //2
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"stats.plist"]; //3

        NSFileManager *fileManager = [NSFileManager defaultManager];

        if (![fileManager fileExistsAtPath: path]) //4
        {
            NSString *bundle = [[NSBundle mainBundle] pathForResource:@"stats" ofType:@"plist"]; //5

            [fileManager copyItemAtPath:bundle toPath: path error:&error]; //6
            [bundle release];
        }

        NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

        //load from savedStock example int value
        score.highScore = [[savedStock objectForKey:@"score"] intValue];
        score.deaths = [[savedStock objectForKey:@"deaths"] intValue];
        score.iFallPoints = [[savedStock objectForKey:@"iFallPoints"] intValue];
        score.difficulty = [[savedStock objectForKey:@"difficulty"] intValue];

        [savedStock release];

score is the singleton I am accessing.

+1  A: 

You didn't alloc or retain bundle; don't release it.

See the memory management programming guide if you need help understanding it.

Also, the static analyzer can be helpful for pointing some out memory bugs.

cobbal