ios Xcode 不显示导致崩溃的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7703052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode doesn't show the line that causes a crash
提问by JonasG
Every time my app crashes Xcode highlights the UIApicationMain() call in the main() function as the line that caused the crash. In some cases that used to be normal (segmentation fault for example) but the crash I am trying to deal with is a simple SIGABRT with detailed information logged in the console:
每次我的应用程序崩溃时,Xcode 都会突出显示 main() 函数中的 UIApicationMain() 调用作为导致崩溃的行。在某些曾经是正常的情况下(例如分段错误),但我试图处理的崩溃是一个简单的 SIGABRT,在控制台中记录了详细信息:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil value (key: Date)'
Xcode used to show the line just right with older SDKs but since i upgraded to Xocde 4.2 that changed. It is pretty obvious that Xcode knows exactly what caused the crash (or could know), but its still not showing the actual line. Is there any fix or workaround for this?
Xcode 曾经在较旧的 SDK 中显示恰到好处的行,但自从我升级到 Xocde 4.2 后,就发生了变化。很明显,Xcode 确切地知道是什么导致了崩溃(或可能知道),但它仍然没有显示实际的行。是否有任何修复或解决方法?
回答by Carter
You should also ensure that you have breakpoints set for all exceptions. This will cause Xcode to stop at the line where the exception is occurring. Do the following [in Xcode 4]:
您还应该确保为所有异常设置了断点。这将导致 Xcode 停止在发生异常的行。执行以下操作 [在 Xcode 4 中]:
In the Project Navigator on the left side of Xcode, click on the breakpoint navigator (almost all the way to the right hand side of the top button bar. The icon looks like a fat right arrow).
At the bottom of the navigator, click the "+" button.
Click "Add Exception Breakpoint".
A new breakpoint will be created. It should be configured as needed but you can tweak its behavior.
Run your project and reproduce the exception.
在 Xcode 左侧的 Project Navigator 中,单击断点导航器(几乎一直到顶部按钮栏的右侧。该图标看起来像一个胖右箭头)。
在导航器底部,单击“+”按钮。
单击“添加异常断点”。
将创建一个新断点。它应该根据需要进行配置,但您可以调整其行为。
运行您的项目并重现异常。
Also you mentioned that you linked to some 3rd party libraries/frameworks. If the exception is occurring within those frameworks then you are going to have a hard time since the code is compiled and Xcode can't actually show you the line that caused the exception. If this is the case and you are certain you are using the libraries correctly, then you should file a bug report to the maintainers of those libraries.
您还提到您链接到了一些 3rd 方库/框架。如果异常发生在这些框架内,那么由于代码已编译并且 Xcode 实际上无法向您显示导致异常的行,因此您将遇到困难。如果是这种情况并且您确定正确使用这些库,那么您应该向这些库的维护者提交错误报告。
回答by Mike Gledhill
Simply follow the instructions on this StackOverflow answer:
只需按照此 StackOverflow 答案中的说明进行操作:
Basically, you just need to "Enable Zombies". Then Xcode should break at whichever line caused the problem.
基本上,您只需要“启用僵尸”。然后Xcode应该在导致问题的任何一行中断。
(It is absolutely shocking that, even in 2017, Xcode still has this turned off by default. Why would you notwant to see the line that caused the problem ? And "Enable Zombie Objects" ?! Really ?! Do the Xcode authors really believe that this is a useful name, which would make any kind of sense to new developers ? It is depressing how poor Xcode's rating is, year after year, in the App Store. No one is listening...)
(这绝对是令人震惊的是,即使是在2017年时,Xcode仍具有默认这个关闭的。为什么你不希望看到导致问题的行?和“启用僵尸对象”?!真的吗?!做Xcode的作者真的相信这是一个有用的名字,它对新开发者有任何意义吗?令人沮丧的是,Xcode 在 App Store 中的评分年复一年地糟糕。没有人在听......)
回答by chown
Edit the current scheme and enable NSZombieEnabled
, MallocStackLogging
, and guard malloc
. Then, when your App crashes, type this in the gdb console:
编辑目前的方案并启用NSZombieEnabled
,MallocStackLogging
和guard malloc
。然后,当您的应用程序崩溃时,在 gdb 控制台中输入:
(gdb) info malloc-history 0x543216
Replace 0x543216
with the address of the object that caused the NSInvalidArgumentException
and it should give you a much more useful stack trace, showing the lines of your code that are causing the crash.
替换为导致崩溃0x543216
的对象的地址,NSInvalidArgumentException
它应该为您提供更有用的堆栈跟踪,显示导致崩溃的代码行。
回答by FluffulousChimp
I have seen this behavior in heavily optimized code; checking,tweaking your target's optimization level and those of 3rd party libs may help. (LLVM 3.0 Optimization level setting)
我在高度优化的代码中看到了这种行为;检查、调整目标的优化级别和第 3 方库的优化级别可能会有所帮助。(LLVM 3.0 优化级别设置)
Are you generating debug symbols?
您正在生成调试符号吗?
回答by AsifHabib
I wrote code to generate a crash of index out of bound. Following is the exception thrown.
我编写了代码来生成索引越界崩溃。以下是抛出的异常。
2017-01-07 04:02:57.606 testABC[1694:52966] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSSingleObjectArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(
0 CoreFoundation 0x000000010e85cd4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010e2be21e objc_exception_throw + 48
2 CoreFoundation 0x000000010e8b5c2f -[__NSSingleObjectArrayI objectAtIndex:] + 111
3 testABC 0x000000010dce962d -[ViewController ComplexFunction] + 61
4 testABC 0x000000010dce95db -[ViewController thirdFunction] + 43
5 testABC 0x000000010dce959b -[ViewController secondFunction] + 43
6 testABC 0x000000010dce955b -[ViewController firstFinction] + 43
7 testABC 0x000000010dce96c2 -[ViewController viewDidAppear:] + 50
8 UIKit 0x000000010ee28a6c -[UIViewController _setViewAppearState:isAnimating:] + 945
9 UIKit 0x000000010ee2b7da __64-[UIViewController viewDidMoveToWindow:shouldAppearOrDisappear:]_block_invoke + 42
10 UIKit 0x000000010ee29ac4 -[UIViewController _executeAfterAppearanceBlock] + 86
11 UIKit 0x000000010ec8d77c _runAfterCACommitDeferredBlocks + 653
12 UIKit 0x000000010ec7a273 _cleanUpAfterCAFlushAndRunDeferredBlocks + 566
13 UIKit 0x000000010ec9d757 __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke_2 + 194
14 CoreFoundation 0x000000010e8016ac __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
15 CoreFoundation 0x000000010e7e66f4 __CFRunLoopDoBlocks + 356
16 CoreFoundation 0x000000010e7e5e65 __CFRunLoopRun + 901
17 CoreFoundation 0x000000010e7e5884 CFRunLoopRunSpecific + 420
18 GraphicsServices 0x00000001126d9a6f GSEventRunModal + 161
19 UIKit 0x000000010ec80c68 UIApplicationMain + 159
20 testABC 0x000000010dce99df main + 111
21 libdyld.dylib 0x000000011174968d start + 1
22 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
If you read carefully the First Throw call stack
如果你仔细阅读 First Throw call stack
0 CoreFoundation 0x000000010e85cd4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010e2be21e objc_exception_throw + 48
0 and 1
are the system processes after crash.
0 and 1
是系统崩溃后的进程。
2 CoreFoundation 0x000000010e8b5c2f -[__NSSingleObjectArrayI objectAtIndex:] + 111
2
is the line which caused the exception.
2
是导致异常的行。
3 testABC 0x000000010dce962d -[ViewController ComplexFunction] + 61
3
tells you that Class name (ViewController
) and function naem (ComplexFunction
) in which exception was thrown.
3
告诉您抛出异常的类名 ( ViewController
) 和函数 naem ( ComplexFunction
)。