`

xcode4 设置调试错误信息小结【转】

 
阅读更多

方案1:NSZombieEnabled

先选中工程, 依次 "Product"-"Edit Scheme", 左栏选择"Run...", 右栏选中Arguments, 然后在Environment Variables下面添加以下三个属性, 设值为YES

NSDebugEnabled

NSZombieEnabled

NSAutoreleaseFreedObjectCheckEnabled

 

 

 有时候在程序出错的时候能准确定位到奔溃的那一行, 或者会给你下面这样的提示,而不仅仅是EXEC_BAD_ACCESS:

 

message sent to deallocated instance 0x126550

 

如果要查看上面地址的分配情况

开启MallocStackLogging(Xcode4勾选下MallocStackLogging就行)

出错时shell malloc_history pid address

另:有时候可以重载respondsToSelector可以帮你找到程序崩溃时最后执行的函数,然后排查.


 

方案2:添加全局断点


Xcode4可以很方便的添加全局的异常断点

 

方案3:中断和未捕获异常

 

1.未拦截信号来源:内核,其他程序,本身.

常见的两个信号:

1).EXC_BAD_ACCESS 试图访问非法内存,导致SIGBUS或者SIGSEGV信号 

2).未能拦截obj_exception_throw导致的SIGABRT信号.

2.方法

1).使用NSUncaughtionHandler安装一个handler拦截未拦截异常

2).使用signal函数安装一个handler拦截BSD信号.(SIGKILL[kill -9]和SIGSTOP[Control+z]无法拦截)

两个c函数

 

  1. void SignalHandler(int signal)  
  2. {  
  3. //中断信号  
  4. }  
  5.   
  6.   
  7. void uncaughtExceptionHandler(NSException *exception)  
  8. {  
  9. //未捕获异常  
  10. }  

 


安装(与全局异常断点冲突,当有这样的断点是,下面拦截函数失效)

  1. void InstallUncaughtExceptionHandler()  
  2. {  
  3.     NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);  
  4.     signal(SIGABRT, SignalHandler);  
  5.     signal(SIGILL, SignalHandler);  
  6.     signal(SIGSEGV, SignalHandler);  
  7.     signal(SIGFPE, SignalHandler);  
  8.     signal(SIGBUS, SignalHandler);  
  9.     signal(SIGPIPE, SignalHandler);  
  10. }  

3.具体实例

1.http://cocoawithlove.com/2010/05/handling-unhandled-exceptions-and.html

重点在于尝试继续运行程序

告诉用户那些因为这些未拦截的异常和信号导致的崩溃,或者自己记录,甚至可以避开这样导致的崩溃.不过,如果多个信号拦截了,这可能失效.

非常推荐看看这篇文章

2.http://parveenkaler.com/2010/08/11/crashkit-helping-your-iphone-apps-suck-less/

重点在于记录异常(之后返回主线程)

  1. - (void)pumpRunLoop  
  2. {  
  3.     self.finishPump = NO;  
  4.     CFRunLoopRef runLoop = CFRunLoopGetCurrent();  
  5.     CFArrayRef runLoopModesRef =     CFRunLoopCopyAllModes(runLoop);  
  6.     NSArray * runLoopModes = (NSArray*)runLoopModesRef;  
  7.     while (self.finishPump == NO)  
  8.     {  
  9.         for (NSString *mode in runLoopModes)  
  10.         {  
  11.             CFStringRef modeRef = (CFStringRef)mode;  
  12.             CFRunLoopRunInMode(modeRef, 1.0f/120.0f, false);  // Pump the loop at 120 FPS  
  13.         }  
  14.     }  
  15.     CFRelease(runLoopModesRef);  
  16. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics