Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I debug EXC_BAD_ACCESS code=2 while using ARC

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.

like image 265
jonathan3087 Avatar asked Feb 16 '13 23:02

jonathan3087


People also ask

What does thread 1 Exc_bad_access mean?

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 Xcode?

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.


1 Answers

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

like image 68
enjayem Avatar answered Sep 18 '22 01:09

enjayem