問題描述
在 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模板網!