Crash log without crash?

♀尐吖头ヾ 提交于 2019-12-04 18:23:49

I have used it couple of times when I had to print stack trace:

+ (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 = UncaughtExceptionHandlerSkipAddressCount;
        i < UncaughtExceptionHandlerSkipAddressCount +
            UncaughtExceptionHandlerReportAddressCount;
        i++)
    {
        [backtrace addObject:[NSString stringWithUTF8String:strs[i]]];
    }
    free(strs);

    return backtrace;
} 

"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." Look at http://cocoawithlove.com/2010/05/handling-unhandled-exceptions-and.html

Here's what I use for my stacktraces:

        NSArray *callStackArray = [exception callStackReturnAddresses];
        int frameCount = [callStackArray count];
        void *backtraceFrames[frameCount];

        for (int i=0; i < frameCount; i++) {
            backtraceFrames[i] = (void *)[[callStackArray objectAtIndex:i] unsignedIntegerValue];
        }

        char **strs = backtrace_symbols(backtraceFrames, frameCount);

        NSMutableArray *backtraceArray = [NSMutableArray arrayWithCapacity:frameCount];
        for (int i = 0; i < frameCount; i++) {
            NSString *entry = [NSString stringWithUTF8String:strs[i]];
            [backtraceArray addObject:entry];
        }
        free(strs);

You just have to make sure you don't do any harm to your app: http://landonf.bikemonkey.org/2011/09/14. You could also use PLCrashReporter to handle all your crashes or if you're lazy like me, use a service like Crittercism

I used to use backtrace_symbols for handling my crashes too, but then I found out that it could be dangerous since the method is not async-safe. I've since looked at a bunch of crash reporting solutions and went with Crittercism for my apps and it's been pretty sweet!

I suggest you check out TestFlight SDK released a few days ago. It has some awesome features like remote logging and even live crash reports.

For an ad hoc version, you could just call the function abort(), or throw and exception of some kind.

An App Store version will not be allowed to ship if it crashes at all during testing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!