简体   繁体   中英

EXC_BAD_ACCESS - iPhone

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM