在 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;
我设置了断点以使我能够检查两个检索到的处理程序值.第一个是 nil,第二个是明显有效的地址.但是当测试异常被抛出时,处理程序(在 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.)
在 iPhone 上设置 NSExceptionHandlingMask
显然是不可能的.先决条件 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.
已经指出,如果你去产品->方案 ->编辑方案,选择运行(调试)",选择诊断";选项卡,然后单击Log Exceptions",这将恢复丢失的 Xcode 默认异常日志记录,可能(我还没有尝试过)消除了对上述 hack 的需要.
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 下控制台中没有异常堆栈跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!