7

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.

jonathan3087
  • 313
  • 1
  • 4
  • 18
  • We need some code to be able to help out. Make sure to include code around the EXC_BAD_ACCESS line. With ARC, a lot of the memory related errors are taken care of automatically by ARC since it allocates and releases and ensure not to overrelease. Having said that, depending on the code logic, it could happen. So please post the code. – Khaled Barazi Feb 17 '13 at 02:20

1 Answers1

22

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

enjayem
  • 941
  • 8
  • 21
  • 1
    It did fix the problem, I no longer get the infinite loop that was causing the many entries in the debug navigator, just like you said. I also resolved the EXC_BAD_ACCESS issue. So now, I am getting a clean build and run with now warnings an no errors. Thank you for your time in looking at this, I was pulling my hair out. – jonathan3087 Feb 17 '13 at 23:05
  • 1
    Thanks for the note on doing stuff "while lazily loading". That still fixed it 5 years later – LinusGeffarth Dec 05 '18 at 11:55