ios 使用 ARC 时如何调试 EXC_BAD_ACCESS code=2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14916332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:20:01  来源:igfitidea点击:

How do I debug EXC_BAD_ACCESS code=2 while using ARC

iosxcodedebuggingnszombie

提问by jonathan3087

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.

我在这里看到很多关于如何解决 EXC_BAD_ACCESS 代码=2 的线程,并且共识似乎是我正在尝试访问我的数组,或者在我已经发布它之后访问我的数组中的对象。我看到大多数情况下,解决方案似乎是他们的代码中有太多 [release theObject]。我的问题是我没有任何发布电话,因为我使用的是 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.

我发现很多线程似乎都说我应该打开 NSZombiesEnabled 以帮助找到问题的根源。

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.

在启用 Zombies 之前,我的应用程序将完全没有错误或警告地构建和运行。但是一旦你触摸模拟器中的按钮,它就会崩溃。

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.

打开 Zombies 后,该应用程序仍然可以构建和运行,没有错误,但是现在模拟器一出现它就会崩溃,现在 XCode 现在切换到 Tread 1 下的 Debug Navigator,其中列出了超过 100,000 个条目,您单击每个条目on 在主窗口显示了一些东西,我不知道是什么意思。

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.

那么,现在我该怎么办?我打开了僵尸,再次运行,我在屏幕上看到一堆东西,但真的不知道如何使它正面或反面。我试图发布屏幕截图,但我还没有权限这样做。

回答by enjayem

I looked through your source code and found the problem. You're trying to set the numberOfMatchingCardsproperty on self.gamewhile you're in the middle of lazily loading game, creating an infinite loop. Your self.game.numberOfMatchingCardsis 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

我查看了您的源代码并发现了问题。当您处于延迟加载过程中时,您试图将numberOfMatchingCards属性设置为on ,从而创建一个无限循环。您将尝试加载一个新游戏,因为在调用 setter 时您还没有完成游戏的实例化。只是改变self.gamegameself.game.numberOfMatchingCards

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

我不确定这是否会解决您的错误访问问题,但这是导致调试导航器中出现许多条目的原因。如果您在修复此问题后仍然遇到访问错误的问题,请告诉我。重要的是要记住,即使您正在使用 ARC,对象仍然会被释放(当它们的引用计数下降到零时)。