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.
EXC_BAD_ACCESS means that message was sent to a point in the memory where there's no instance of a class to execute it. Thus “bad access”. You will get EXC_BAD_ACCESS in 3 cases: An object is not initialized.
What does EXC_BAD_ACCESS mean? EXC_BAD_ACCESS is an exception raised as a result of accessing bad memory. We're constantly working with pointers to memory in Swift that link to a specific memory address. An application will crash whenever we try to access a pointer that is invalid or no longer exists.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With