如何防止EXC_BAD_ACCESS崩溃的应用程序? [英] How to prevent EXC_BAD_ACCESS from crashing an app?

查看:81
本文介绍了如何防止EXC_BAD_ACCESS崩溃的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以防止EXC_BAD_ACCESS使应用程序崩溃,就像使用@try..@catch一样,您可以优雅地处理异常.

Is there a way to prevent an EXC_BAD_ACCESS from crashing an app, like with @try..@catch you can handle an exception gracefully.

更新:

当尝试取消引用无效的指针时,代码崩溃.这是一个第三方库,它与外部硬件连接,因此我无法在本地调试.我试图防止它崩溃并将数据输出到我的应用程序上的调试控制台.

The code crashes when it attempts to dereference an invalid pointer. This is a third party library and it interfaces with external hardware so I can't debug locally. I am trying to prevent it from crashing and output data to a debugging console on my app.

推荐答案

在ObjC中,try/catch不能特别优雅地处理异常.您仍然会泄漏内存并使系统保持未定义状态.除极少数情况外,期望是您只是在追赶,这样您就可以在崩溃之前记录一些事情.通常,您不应在程序的顶层使用@catch来达到此目的.在某些特殊情况下,限制使用例外可能是适当的,但是在ObjC中很少见.有关更多信息,请参见异常编程指南信息.尤其请参见 ObjC ARC文档

In ObjC, try/catch do not handle exceptions particularly gracefully. You will still leak memory and leave the system in an undefined state. With rare exception, the expectation is that you are simply catching so you can log some things before crashing. And in general, you should not use @catch anywhere but at the top level of your program for this purpose. There are some extraordinary situations where limited use of exceptions may be appropriate, but they are rare in ObjC. See the Exception Programming Guide for some more information. See especially the following from the ObjC ARC documentation:

标准的Cocoa约定是异常表示程序员错误,并且不能从异常中恢复.默认情况下,使代码异常安全将对通常实际上并不关心异常安全的代码施加严重的运行时和代码大小的损失.因此,默认情况下,由ARC生成的代码会因异常而泄漏,如果该过程无论如何都将立即终止,则很好.关心从异常中恢复的程序应启用选项[-fobjc-arc-exceptions,这会在程序上施加速度和内存惩罚].

The standard Cocoa convention is that exceptions signal programmer error and are not intended to be recovered from. Making code exceptions-safe by default would impose severe runtime and code size penalties on code that typically does not actually care about exceptions safety. Therefore, ARC-generated code leaks by default on exceptions, which is just fine if the process is going to be immediately terminated anyway. Programs which do care about recovering from exceptions should enable the option [-fobjc-arc-exceptions, which imposes speed and memory penalties on your program].

EXC_BAD_ACCESS也是如此.您可以使用信号处理程序来捕获它,以记录一些信息,然后完成崩溃.有关执行此操作的好工具,请参见 PLCrashReporter .正确编写这样的处理程序非常困难,因此我强烈建议您使用现有框架.如果您不正确地捕获EXC_BAD_ACCESS,您很容易陷入死机,耗尽用户的电量.

The same is true of EXC_BAD_ACCESS. You can catch it with a signal handler for the purpose of recording some information and then finishing your crash. For a good tool for doing this see PLCrashReporter. It is very difficult to write such a handler correctly, so I strongly recommend using an existing framework. You can easily get into deadlocks that drain the user's battery if you catch EXC_BAD_ACCESS incorrectly.

这篇关于如何防止EXC_BAD_ACCESS崩溃的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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