Xcode 4.2 / iOS 5下的控制台中没有异常堆栈跟踪? [英] No exception stacktrace in console under Xcode 4.2/iOS 5?

查看:105
本文介绍了Xcode 4.2 / iOS 5下的控制台中没有异常堆栈跟踪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Xcode 3.x和iOS 4下,如果在模拟器中发出未处理的异常信号,则在控制台输出中会产生异常堆栈跟踪(类似于Java)。

Under Xcode 3.x and iOS 4, if an unhandled exception is signaled in the emulator there is an exception stack trace (similar to Java's) produced in the console output.

当我在Xcode 4.2下的iOS 5中引发未处理的异常时,运行完全相同的应用程序代码时,不会发生堆栈跟踪。 (我确实弄清楚如何设置异常断点,但这不会在控制台中产生回溯。)

When I raise an unhandled exception in iOS 5 under Xcode 4.2, running the exact same app code, the stack trace does not occur. (I did figure out how to set an exception breakpoint, but that doesn't produce the traceback in the console.)

这只是我需要制作的Xcode设置某处,还是Xcode 4 / iOS 5的功能?有没有办法恢复这一点功能?

Is this merely an Xcode setting I need to make somewhere, or a "feature" of Xcode 4/iOS 5? Is there some way restore this bit of functionality?

不幸的是,添加 uncaughtExceptionHandler 不起作用。这是处理程序:

Unfortunately, adding an uncaughtExceptionHandler doesn't work. Here is the handler:

void uncaughtExceptionHandler(NSException *exception) {
    NSLog(@"uncaughtExceptionHnadler -- Exception %@", [exception description]);
    // Because iOS 5 doesn't provide a traceback, provide one here
    NSLog(@"Stack trace: %@", [exception callStackSymbols]);
    // Let Flurry look at the error
    [FlurryAPI logError:@"Uncaught" message:@"Crash!" exception:exception];
}                                               

(事实证明它已经存在,做Flurry的事情,所以我刚刚添加了堆栈跟踪。)

(It turns out it was already present, to do the Flurry thing, so I just added the stack trace.)

这是启用它的地方(只在声明处理程序的下面几行):

Here is where it's enabled (just a few lines below where the handler is declared):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Enable uncaught exception handler to dump stack and let Flurry log the exception
    NSUncaughtExceptionHandler* hdlr = NSGetUncaughtExceptionHandler();
    NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
    NSUncaughtExceptionHandler* newHdlr = NSGetUncaughtExceptionHandler();

    // TODO: Test
    NSException* ex = [NSException exceptionWithName:@"AssertionFailure" reason:@"Test" userInfo:nil]; 
    @throw ex; 

我设置断点使我能够检查两个检索到的处理程序值。第一个是零,第二个是明显有效的地址。但是当抛出测试异常时,处理程序(在iOS 5模拟器中)永远不会得到控制。 (虽然当我在iOS 4.2模拟器上运行时它确实可以控制。)

I set breakpoints to enable me to check the two retrieved handler values. The first one is nil and the second is an apparently valid address. But when the test exception is thrown the handler (in iOS 5 simulator) never gets control. (Though when I run on the iOS 4.2 simulator it does get control.)

设置 NSExceptionHandlingMask 显然是不可能的在iPhone上。先决条件 ExceptionHandling.framework 不可用。

Setting NSExceptionHandlingMask is apparently not possible on iPhone. The prereq ExceptionHandling.framework is not available.

这有效:

int main(int argc, char *argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = -1;
    @try {
        retVal = UIApplicationMain(argc, argv, nil, nil);
    }
    @catch (NSException* exception) {
        NSLog(@"Uncaught exception: %@", exception.description);
        NSLog(@"Stack trace: %@", [exception callStackSymbols]);
    }
    [pool release];
    return retVal;
}


推荐答案

这有效:

int main(int argc, char *argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = -1;
    @try {
        retVal = UIApplicationMain(argc, argv, nil, nil);
    }
    @catch (NSException* exception) {
        NSLog(@"Uncaught exception: %@", exception.description);
        NSLog(@"Stack trace: %@", [exception callStackSymbols]);
    }
    [pool release];
    return retVal;
}

对于ARC:

int main(int argc, char *argv[]) {

    int retVal = -1;
    @autoreleasepool {
        @try {
            retVal = UIApplicationMain(argc, argv, nil, nil);
        }
        @catch (NSException* exception) {
            NSLog(@"Uncaught exception: %@", exception.description);
            NSLog(@"Stack trace: %@", [exception callStackSymbols]);
        }
    }
    return retVal;
}

还在等待某种解释,说明为什么默认转储不再有效和/或为什么(甚至更严重)uncaughtExceptionHandler不起作用。但是,显然这个问题只影响模拟器。

Still waiting for some sort of explanation as to why the default dump no longer works and/or why (even more serious) uncaughtExceptionHandler doesn't work. However, apparently this problem only affects the emulator.

有人指出,如果你转到Product - > Scheme - > Edit Scheme,选择Run(Debug),选择Diagnostics选项卡,然后单击Log Exceptions,这将恢复丢失的Xcode默认异常日志记录,可能(我还没试过)它还没有上述黑客的需要。

It has been pointed out that if you go to Product -> Scheme -> Edit Scheme, select "Run (Debug)", select the "Diagnostics" tab, and click "Log Exceptions", this will restore the missing Xcode default exception logging, possibly (I haven't tried it yet) eliminating the need for the above hack.

这篇关于Xcode 4.2 / iOS 5下的控制台中没有异常堆栈跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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