给定线程 1 exc_bad_access(代码=2 地址=0x8),Xcode 不断崩溃

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21212988/
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-09-15 04:34:17  来源:igfitidea点击:

Xcode continuously crashes given thread 1 exc_bad_access (code=2 address=0x8)

iosxcodeios7crashxcode5

提问by mhamer13

after researching this error i have noticed it is code specific but any project i attempt to make, load, etc keeps giving me the same error and is always citing different parts of the code with this error message but because of the initial programs i setup, that have the exact default code nothing less nothing more and still receive the same errors i wouldn't see how adding my error log could help. Note this error occurs when using sprite kit in Xcode 5 and am running this on os x 10.8.5 thanks ahead of time for your patience, insight and hopefully an answer.

在研究了这个错误之后,我注意到它是特定于代码的,但是我尝试制作、加载等的任何项目都会给我同样的错误,并且总是用这个错误消息引用代码的不同部分,但是由于我设置的初始程序,具有确切的默认代码,仅此而已,并且仍然收到相同的错误,我看不出添加错误日志有何帮助。请注意,在 Xcode 5 中使用 sprite kit 并在 os x 10.8.5 上运行此错误时会发生此错误,提前感谢您的耐心、洞察力并希望得到答案。

回答by Jasper Blues

exc_bad_access is usually caused by what's known as a dangling pointer - accessing an object that has already been deallocated.

exc_bad_access 通常是由所谓的悬空指针引起的——访问一个已经被释放的对象。

To debug this, enable zombie objects by either:

要对此进行调试,请通过以下任一方式启用僵尸对象:

  • Xcode : Edit the scheme and choose the 'Enabled Zombies' checkbox
  • Xcode:编辑方案并选择“启用僵尸”复选框

Edit Scheme

编辑方案

  • AppCode : Edit the run configuration and set the environment variable NS_ZOMBIE_ENABLED=YES
  • AppCode : 编辑运行配置并设置环境变量 NS_ZOMBIE_ENABLED=YES

Spend some time to learn the Objective-C reference counting memory model - retains, releases, retain-cycles and autorelease pools.

花一些时间学习 Objective-C 引用计数内存模型 - 保留、释放、保留循环和自动释放池。

You need to ensure that your object is being retained for as long as its being used. In ARC (Automatic Reference Counting - the default for iOS) this usually means setting a property or ivar for any object that will be used accross multiple classes - designating one object as the 'owner' of this object. It will then be deallocated along with the object that 'owns' it.

您需要确保您的对象在使用期间一直保留。在 ARC(自动引用计数 - iOS 的默认设置)中,这通常意味着为将在多个类中使用的任何对象设置属性或 ivar - 将一个对象指定为该对象的“所有者”。然后它将与“拥有”它的对象一起被释放。

A good way to get an understanding of the Objective-C memory model and the way ARC works, is to try using manual memory management on a pet project (something with at least 2 or 3 view controllers). Once you're comfortable with manual-memory management, ARC will be super-easy, as well as save you time, typing and prevent forgetting to release an allocated object. Most/all Objective-C fundamentals books will cover memory management. (I've forgotten the name now of the one that I read).

了解 Objective-C 内存模型和 ARC 工作方式的一个好方法是尝试在宠物项目(至少有 2 或 3 个视图控制器的项目)上使用手动内存管理。一旦您对手动内存管理感到满意,ARC 将变得非常简单,并且可以节省您的时间、打字和防止忘记释放分配的对象。大多数/所有 Objective-C 基础书籍都将涵盖内存管理。(我现在忘记了我读过的那个人的名字)。

A Common Cause

一个共同的原因

Let's say you have a UIViewController and a View that you'd like to present from within your current view controller. . . its allocated as follows:

假设您有一个 UIViewController 和一个想要从当前视图控制器中呈现的视图。. . 其分配如下:

UIViewController* anotherController = [UIViewController make];
[self.view addSubView anotherController.view];

'anotherController' will now be deallocated because its no longer used. If anotherController.view has unsafe_unretained references to anotherController it will cause an exc_bad_access.

'anotherController' 现在将被释放,因为它不再使用。如果 anotherController.view 有对 anotherController 的 unsafe_unretained 引用,它将导致 exc_bad_access。

Infinite Recursion:

无限递归:

Another cause of EXC_BAD_ACCESS is infinite recurssion, which can be debugged by adding some log statements - it will soon become apparent then!

EXC_BAD_ACCESS 的另一个原因是无限递归,可以通过添加一些日志语句来调试——这很快就会变得明显!