收到 EXC_BAD_ACCESS 信号 [英] EXC_BAD_ACCESS signal received

查看:26
本文介绍了收到 EXC_BAD_ACCESS 信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When deploying the application to the device, the program will quit after a few cycles with the following error:

Program received signal: "EXC_BAD_ACCESS".

该程序在 iPhone 模拟器上运行没有任何问题,只要我一次一个地执行指令,它也会调试和运行.一旦我让它再次运行,我就会触发 EXC_BAD_ACCESS 信号.

The program runs without any issue on the iPhone simulator, it will also debug and run as long as I step through the instructions one at a time. As soon as I let it run again, I will hit the EXC_BAD_ACCESS signal.

在这种特殊情况下,它碰巧是加速度计代码中的错误.它不会在模拟器中执行,这就是它没有抛出任何错误的原因.但是,它会在部署到设备后执行.

In this particular case, it happened to be an error in the accelerometer code. It would not execute within the simulator, which is why it did not throw any errors. However, it would execute once deployed to the device.

这个问题的大部分答案都涉及一般的 EXC_BAD_ACCESS 错误,所以我将把它作为可怕的错误访问错误的全部内容保留下来.

Most of the answers to this question deal with the general EXC_BAD_ACCESS error, so I will leave this open as a catch-all for the dreaded Bad Access error.

EXC_BAD_ACCESS 通常是由于非法内存访问而引发的.您可以在下面的答案中找到更多信息.

EXC_BAD_ACCESS is typically thrown as the result of an illegal memory access. You can find more information in the answers below.

您之前是否遇到过EXC_BAD_ACCESS 信号,您是如何处理的?

Have you encountered the EXC_BAD_ACCESS signal before, and how did you deal with it?

推荐答案

根据您的描述,我怀疑最可能的解释是您的内存管理存在错误.你说你已经在 iPhone 开发上工作了几个星期,但不是你是否对 Objective C 有总体经验.如果您来自其他背景,可能需要一段时间才能真正内化内存管理规则 - 除非您非常重视.

From your description I suspect the most likely explanation is that you have some error in your memory management. You said you've been working on iPhone development for a few weeks, but not whether you are experienced with Objective C in general. If you've come from another background it can take a little while before you really internalise the memory management rules - unless you make a big point of it.

请记住,您从分配函数(通常是静态分配方法,但也有其他一些方法)或复制方法获得的任何内容,您也拥有内存,并且必须在完成后释放它.

Remember, anything you get from an allocation function (usually the static alloc method, but there are a few others), or a copy method, you own the memory too and must release it when you are done.

但是如果你从几乎任何其他包括工厂方法(例如[NSString stringWithFormat])中得到一些东西,那么你将有一个自动释放引用,这意味着它可能会在将来的某个时间由其他代码发布 - 因此,如果您需要将其保留在您保留它的直接功能之外,这一点至关重要.如果您不这样做,则在您使用它时,内存可能会保持分配状态,或者在您的模拟器测试期间被释放但碰巧仍然有效,但更有可能被释放并在设备上运行时显示为错误访问错误.

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 选项.

The best way to track these things down, and a good idea anyway (even if there are no apparent problems) is to run the app in the Instruments tool, especially with the Leaks option.

这篇关于收到 EXC_BAD_ACCESS 信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆