iPhone - 辅助线程崩溃不会影响主线程
我注意到,当我的应用程序在主线程上崩溃时,在设备上运行时,程序会停止,并且我会在 XCode 上获取调用堆栈,这样我就可以准确地看到崩溃发生的位置/原因。
但是,当崩溃发生在辅助线程上时,主线程(gui)继续正常运行,我只在控制台上收到有关崩溃的消息,而没有整个调用堆栈。
为了验证它,我调用了一个不存在的方法,一次在主线程上,一次在辅助线程上,实际上,当辅助线程崩溃时,我只在控制台中看到一条有关“无法识别的选择器”的消息。
有没有办法让辅助线程使应用程序崩溃并显示完整堆栈?这对于调试目的非常有用。
谢谢
I'v noticed that when my app crashes on the main thread , while running on device, the program stops and I get the call stack on XCode so I can see exactly where/why the crash happend.
However, when the crash happens on a secondary thread, the main thread (gui) continue to run normally and I only get message on the console about the crash, without the entire call stack.
To verify it I called a method which doesn't exist, once on the main thread on once on a secondary thread, and indeed when the secondary thread crash I only see a message in the console about the "unrecognized selector".
Is there a way to make the secondary thread crash the app and showing the full stack as well ? It would be very useful for debug purposes.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
结果我没有看到崩溃的原因是我有 try&catch 语句......必须留意它们。
Turned out the reason I didn't see the crash was that I had try&catch statements...gotta watch out for them.
您应该在第二个线程的代码周围放置一个 try...catch 并在 catch 中转储完整的异常描述。
尽管对此不熟悉 iPhone 的内部结构,但请记住主线程只是一个运行各种方法的循环。对于系统在主循环上执行的任何代码,它很可能在 try...catch 中执行该代码,以便它可以记录问题,并有可能优雅地退出应用程序。
You should put a try...catch around the second thread's code and just dump the full exception description in the catch.
Although unfamiliar with the internals of the iPhone for this, remember that the main thread is just a loop that is running various methods. For any code that the system executes on the main loop, it most likely executes that code in a try...catch so it can log the problem, and, potentially, exit the app gracefully.