exc bad access iphone error
I am getting an error called Thread 1: Program recieved signal:"EXC_BAD_ACCESS" here is my code
[gameArray removeLastObject];
[gameArray addObject:shotArray];
[gamesArray removeObjectAtInd开发者_开发技巧ex:gameNumber];
[gamesArray insertObject:gameArray atIndex:gameNumber];
NSString *path = [self findGamesPath];
[NSKeyedArchiver archiveRootObject:gamesArray toFile:path]; // the error is here
Why is there an error? Is something being released too many times?
Here is the findGamesPath code
-(NSString *)findGamesPath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentFolder = [paths objectAtIndex:0]; NSString *result = [documentFolder stringByAppendingPathComponent:@"iShotTrackGames.plist"]; return result; }
More likely than not, something is being released too many times. Turn on zombie detection and try again.
Before you do that, though, try performing a "Build and Analyze" on your code and fix any problems it identifies.
I see this is tagged with "thread-safety". Why? I.e. what are other threads potentially doing while the above code is running?
EXC_BAD_ACCESS means you've got a bad pointer somewhere. These errors are often pretty easy to spot because the bad pointer is one of the parameters to the method called on the line where the error occurs. In this case, though, the archiver is going to walk the entire object graph pointed to by gamesArray, and the bad pointer could really be anywhere in there. Follow @bbum's advice to turn on NSZombies -- that'll help you figure out which pointer is the problem.
I think I know what's happening. Your path string is losing scope inside the findGamePath method.
Are you allocating the path string inside that method and returning it?
Once it loses scope it's released from memory.
Trying to access that string then will cause EXC_BAD_ACCESS as you're accessing a released object.
Could you post the findGamePath code here? That may clarify the issue more.
Oh and this has more to do with memory management than thread safety.
精彩评论