当将应用程序部署到设备时,程序将在几个周期后退出,并出现以下错误:

Program received signal: "EXC_BAD_ACCESS".

程序在iPhone模拟器上运行没有任何问题,只要我一次执行一个指令,它也会调试和运行。一旦我让它再次运行,我将击中EXC_BAD_ACCESS信号。

在这种特殊情况下,它恰好是加速度计代码中的一个错误。它不会在模拟器中执行,这就是它不会抛出任何错误的原因。但是,它将在部署到设备后执行。

这个问题的大多数答案都处理一般的EXC_BAD_ACCESS错误,因此我将保留这个选项,作为可怕的坏访问错误的统称。

EXC_BAD_ACCESS通常是非法内存访问的结果。你可以在下面的答案中找到更多信息。

您以前遇到过EXC_BAD_ACCESS信号吗?您是如何处理它的?


当前回答

从你的描述中,我怀疑最有可能的解释是你在内存管理中出现了一些错误。你说你已经在iPhone开发上工作了几个星期,但没有说你是否对Objective C有一般的经验。如果你来自其他背景,在你真正内化内存管理规则之前可能需要一段时间——除非你把它说得很重要。

记住,你从分配函数(通常是静态的alloc方法,但也有一些其他方法)或复制方法获得的任何东西,你也拥有内存,当你完成时必须释放它。

But if you get something back from just about anything else including factory methods (e.g. [NSString stringWithFormat]) then you'll have an autorelease reference, which means it could be released at some time in the future by other code - so it is vital that if you need to keep it around beyond the immediate function that you retain it. If you don't, the memory may remain allocated while you are using it, or be released but coincidentally still valid, during your emulator testing, but is more likely to be released and show up as bad access errors when running on the device.

追踪这些东西的最好方法,也是一个好主意(即使没有明显的问题)是在Instruments工具中运行应用程序,特别是带有Leaks选项。

其他回答

这是一个很好的帖子。以下是我的经验:我在属性声明中搞砸了retain/assign关键字。我说:

@property (nonatomic, assign) IBOutlet UISegmentedControl *choicesControl;
@property (nonatomic, assign) IBOutlet UISwitch *africaSwitch;
@property (nonatomic, assign) IBOutlet UISwitch *asiaSwitch;

我该说什么来着

@property (nonatomic, retain) IBOutlet UISegmentedControl *choicesControl;
@property (nonatomic, retain) IBOutlet UISwitch *africaSwitch;
@property (nonatomic, retain) IBOutlet UISwitch *asiaSwitch;

我得到它,因为我没有使用[self performSegueWithIdentifier:sender:]和-(void) prepareForSegue:(UIstoryboardSegue *)正确

在我的例子中,我有一个视图(a),其中我有另一个视图(B)的实例。我忘记了B是a的子类,显然这导致了递归和无休止的分配。修复了EXE_BAD_ACCESS问题。

   class A: UIView {
 
      let b = B()
      .
      .
   }



   class B: A {
     .
     .
   }

忘记从dealloc中取出一个非alloc指针。我在我的UINavigationController的rootView上获得了exc_bad_access,但只是有时。我假设问题出在rootView上,因为它在viewDidAppear{}中崩溃了。它只发生在我用坏的dealloc{}发布弹出视图之后,这就是它!

"EXC_BAD_ACCESS"[切换到进程330]现在没有可用的内存来编程:调用malloc不安全

我认为这是一个问题,我试图分配…不是在我试图释放非alloc的地方,天哪!

希望您在完成时释放“字符串”!