How do I debug EXC_BAD_ACCESS code=2 while using ARC

jonathan3087 picture jonathan3087 · Feb 17, 2013 · Viewed 29k times · Source

I see alot of threads on here about how to solve the EXC_BAD_ACCESS code=2, and the consensus seems to be that I am trying to access my array, or an object in my array after I have already released it. I see that most of the time the solutions seems to be that one has too many [release theObject] in their code. The problem for me is that I don't have any release calls, because I am using ARC.

So my question is how do I go about debugging this myself from this point. I can post code if that would help, but I think as a first step, I'd just like help on what my next step should be and how to do it.

I have found that a lot of threads seem to say that I should turn on NSZombiesEnabled to help find the source of the problem.

Before Zombies were enabled my app would build and run with absolutely no error or warnings. But as soon as you touch a button in the simulator it would crash.

After turning on Zombies, the app still builds and runs with no errors, but it now crashes as soon as the simulator appears, and now XCode now switches to the Debug Navigator under Tread 1 there are listed a over 100,000 entries and each one you click on shows some stuff in the main window, which I don't know what means.

So, now what do I do? I have turned on Zombies, and run again, I see a bunch of stuff in the screen, but don't really know how to make heads or tails of it. I tried to post a screen shot, but I don't have the authority to do it yet.

Answer

enjayem picture enjayem · Feb 17, 2013

I looked through your source code and found the problem. You're trying to set the numberOfMatchingCards property on self.game while you're in the middle of lazily loading game, creating an infinite loop. Your self.game.numberOfMatchingCards is going to try to load a new game since you haven't finished instantiating the game by the time your setter is called. Just change

- (IBAction)cardModeChanged:(UISegmentedControl *)sender {
    switch ([sender selectedSegmentIndex]) {
        case 0:
            self.game.numberOfMatchingCards = 2;
            break;
        case 1:
            self.game.numberOfMatchingCards = 3;
            break;
        default:
            self.game.numberOfMatchingCards = 2;
            break;
    }
}

to

- (IBAction)cardModeChanged:(UISegmentedControl *)sender {
    switch ([sender selectedSegmentIndex]) {
        case 0:
            _game.numberOfMatchingCards = 2;
            break;
        case 1:
            _game.numberOfMatchingCards = 3;
            break;
        default:
            _game.numberOfMatchingCards = 2;
            break;
    }
}

I'm not sure this will solve your bad access issue, but it's the cause of the many entries in the debug navigator. Let me know if you still have the bad access issue after fixing this. It's important to remember that even though you're using ARC, objects still get released (when their reference counts drop to zero).