闪退捕捉办法
来源:互联网 发布:淘宝运营周计划表 编辑:程序博客网 时间:2024/04/29 11:25
@interface ExceptionHandlers : NSObject{
BOOL dismissed;
}
@end
void HandleException(NSException *exception);
void SignalHandler(int signal);
void InstallUncaughtExceptionHandler(void);
#import "ExceptionHandlers.h"
#import <libkern/OSAtomic.h>
#import <execinfo.h>
#import "ASIFormDataRequest.h"
#import "SBJson.h"
NSString * const UncaughtExceptionHandlerSignalExceptionNames = @"UncaughtExceptionHandlerSignalExceptionName";
NSString * const UncaughtExceptionHandlerSignalKeys = @"UncaughtExceptionHandlerSignalKey";
NSString * const UncaughtExceptionHandlerAddressesKeys = @"UncaughtExceptionHandlerAddressesKey";
volatile int32_t UncaughtExceptionCounts = 0;
const int32_t UncaughtExceptionMaximums = 10;
const NSInteger UncaughtExceptionHandlerSkipAddressCounts = 4;
const NSInteger UncaughtExceptionHandlerReportAddressCounts = 5;
NSMutableDictionary *dic;
@interface ExceptionHandlers()<UIAlertViewDelegate>
@end
@implementation ExceptionHandlers
+ (NSArray *)backtrace
{
void* callstack[128];
int frames = backtrace(callstack, 128);
char **strs = backtrace_symbols(callstack, frames);
int i;
NSMutableArray *backtrace = [NSMutableArray arrayWithCapacity:frames];
for (
i = UncaughtExceptionHandlerSkipAddressCounts;
i < UncaughtExceptionHandlerSkipAddressCounts +
UncaughtExceptionHandlerReportAddressCounts;
i++)
{
[backtrace addObject:[NSString stringWithUTF8String:strs[i]]];
}
free(strs);
return backtrace;
}
- (void)alertView:(UIAlertView *)anAlertView clickedButtonAtIndex:(NSInteger)anIndex
{
if (anIndex == 0)
{
dismissed = YES;
NSLog(@"click cancel");
}
else
NSLog(@"click 确认");
}
- (void)validateAndSaveCriticalApplicationData
{
}
//异常处理方法,可以用来打印、保存、上传错误信息;
- (void)handleException:(NSException *)exception
{
dic=[NSMutableDictionary dictionary];
dic[@"addDate"]=[NSDate date];
dic[@"reason"]=[exception reason];
dic[@"userInfo"]=[[exception userInfo] objectForKey:UncaughtExceptionHandlerAddressesKeys];
[self validateAndSaveCriticalApplicationData];
NSString *nonormalString = [NSString stringWithFormat:@"%@-%@-%@",dic[@"addDate"], dic[@"reason"], dic[@"userInfo"]];
NSlog(@"%@",nonormalString);//打印错误信息
CFRunLoopRef runLoop = CFRunLoopGetCurrent();
CFArrayRef allModes = CFRunLoopCopyAllModes(runLoop);
dismissed=YES;
while (!dismissed)
{
for (NSString *mode in (__bridge NSArray*)allModes)
{
CFRunLoopRunInMode((CFStringRef)mode, 0.001, false);
}
}
CFRelease(allModes);
NSSetUncaughtExceptionHandler(NULL);
signal(SIGABRT, SIG_DFL);
signal(SIGILL, SIG_DFL);
signal(SIGSEGV, SIG_DFL);
signal(SIGFPE, SIG_DFL);
signal(SIGBUS, SIG_DFL);
signal(SIGPIPE, SIG_DFL);
if ([[exception name] isEqual:UncaughtExceptionHandlerSignalExceptionNames])
{
kill(getpid(), [[[exception userInfo] objectForKey:UncaughtExceptionHandlerSignalKeys] intValue]);
}
else
{
[exception raise];
}
}
@end
void HandleException(NSException *exception)
{
int32_t exceptionCount = OSAtomicIncrement32(&UncaughtExceptionCounts);
if (exceptionCount > UncaughtExceptionMaximums)
{
return;
}
NSArray *callStack = [ExceptionHandlers backtrace];
NSMutableDictionary *userInfo =
[NSMutableDictionary dictionaryWithDictionary:[exception userInfo]];
[userInfo
setObject:callStack
forKey:UncaughtExceptionHandlerAddressesKeys];
[[[ExceptionHandlers alloc] init]
performSelectorOnMainThread:@selector(handleException:)
withObject:
[NSException
exceptionWithName:[exception name]
reason:[exception reason]
userInfo:userInfo]
waitUntilDone:YES];
}
void SignalHandler(int signal)
{
int32_t exceptionCount = OSAtomicIncrement32(&UncaughtExceptionCounts);
if (exceptionCount > UncaughtExceptionMaximums)
{
return;
}
NSMutableDictionary *userInfo =
[NSMutableDictionary
dictionaryWithObject:[NSNumber numberWithInt:signal]
forKey:UncaughtExceptionHandlerSignalKeys];
NSArray *callStack = [ExceptionHandlers backtrace];
[userInfo
setObject:callStack
forKey:UncaughtExceptionHandlerAddressesKeys];
[[[ExceptionHandlers alloc] init]
performSelectorOnMainThread:@selector(handleException:)
withObject:
[NSException
exceptionWithName:UncaughtExceptionHandlerSignalExceptionNames
reason:
[NSString stringWithFormat:
NSLocalizedString(@"Signal %d was raised.", nil),
signal]
userInfo:
[NSDictionary
dictionaryWithObject:[NSNumber numberWithInt:signal]
forKey:UncaughtExceptionHandlerSignalKeys]]
waitUntilDone:YES];
}
void InstallUncaughtExceptionHandler(void)
{
NSSetUncaughtExceptionHandler(&HandleException);
signal(SIGABRT, SignalHandler);//abort
signal(SIGILL, SignalHandler);
signal(SIGSEGV, SignalHandler);
signal(SIGFPE, SignalHandler);
signal(SIGBUS, SignalHandler);
signal(SIGPIPE, SignalHandler);
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
InstallUncaughtExceptionHandler(); //启动异常捕捉
}
- 闪退捕捉办法
- iReport闪退问题解决办法
- tomcat闪退问题解决办法
- 解决eclipse闪退的办法
- 解决eclipse闪退的办法
- 解决Eclipse闪退的最好办法
- 解决eclipse闪退的办法
- Ubuntu 16.04 software闪退处理办法
- [ubuntu] 16.04 software闪退处理办法
- 解决eclipse闪退的办法
- 扫码。摄像捕捉。AVCaptureSession初始化 闪退。
- 解决iPhone完美越狱打开天气闪退的办法
- 解决Eclipse启动闪退的最佳办法
- tomcat双击startup.bat闪退的办法
- windows编程 控制台程序闪退,一闪而退 怎么处理 处理办法
- Matlab运行错误捕捉办法
- cocos2d-x3.0rc第一次移植闪退的处理办法
- vs2010 C++ win32控制台程序窗口闪退问题解决办法汇总
- 开始学习Python,设置环境和编译一个简单的程序
- 恒拓开源笔试题
- Brackets安装Emmet插件显示“Internal error”解决办法
- java.io.Serializable接口
- PL/SQL Developer Logon the DB user with ORA-12154
- 闪退捕捉办法
- service介绍之service的使用与本地通信
- 【SDOI2013】淘金
- setNeedsDisplay setNeedsLayout layoutIfNeeded
- 《电子或通信领域当前的主流技术及其社会需求调查报告》
- 入门训练 圆的面积
- 函数对象(function object)的简单讲解
- ubuntu下用 rvm 安装 ruby rails 小记
- 七、常量指针和指针常量