When an application crashes on the iPhone, it disappears without telling the user what happened. However, it is possible to add exception and signal handling
to your applications so that an error message can be displayed to the user or you can save changes. It is even possible to try to recover from this situation without crashing at all.
从某种意义来说,调试,捕捉signal或则crash memory issue是正常程序不应该处理的事情,主要通过非正当渠道可解决的cras避免的
h
接下来介绍的方法能够搞定BAD——Access,以及BSD singal,甚至程序还能继续运行
EXC_BAD_ACCESS
exceptions
and related BSD signals. All exceptions and signals are caught, presenting debug information and allowing the application to continue after these events.
我们看两类型的exception
An unhandled signal can come from three places: the kernel, other processes or the application itself. The two most common signals that cause crashes are:
EXC_BAD_ACCESS
is a Mach exception sent by the kernel to your application when you try to access memory that is not mapped for your application. If not handled at the Mach level, it will
be translated into aSIGBUS
orSIGSEGV
BSD signal.SIGABRT
is a BSD signal sent by an application to itself when anNSException
orobj_exception_throw
is
not caught.
捕获非捕捉的异常
最正确的处理方式是找到原因并解决crash,如果您的程序一直运行正常,是不需要下面的手段的:
当然,程序往往发布的时候带有一些非常隐蔽的crash问题,然后你又想收集跟多的信息:
一般来说两个方法:使用
NSUncaughtExceptionHandler以及sinal
In these cases, there are two ways to catch otherwise uncaught conditions that will lead to a crash:
- Use the function
NSUncaughtExceptionHandler
to install a handler for uncaught Objective-C exceptions. - Use the
signal
function to install handlers for BSD signals. - //从代码来看,是非常简单的
void
{ NSSetUncaughtExceptionHandler(&HandleException); signal( SIGABRT , signal( SIGILL , signal( SIGSEGV , signal( SIGFPE , signal( SIGBUS , signal( SIGPIPE , } |
the run loop must handle all the modes of the main run loop. Since the main run loop includes a few private modes (for GSEvent handling and scroll tracking), the defaultNSDefaultRunLoopMode
is
insufficent.
Fortunately, if the UIApplication
has already created all the modes for the main loop, then we can get all of these modes by reading from the loop. Assuming it is run on the main thread after
the main loop is created, the following code will run the loop in all UIApplication
modes:
|