【问题标题】:Debugging UIKit crash [UINavigationController initWithRootViewController]调试 UIKit 崩溃 [UINavigationController initWithRootViewController]
【发布时间】:2015-10-01 16:06:53
【问题描述】:

Xcode 7.0.1

更新:

我尝试的最新方法是分解 UINavigationController 的创建:

  self.viewController = [[ProjectsViewController alloc] initWithNibName:@"ProjectsViewController_iPhone" bundle:nil];
  self.navigationController = [[UINavigationController alloc] init];
  [self.navigationController setViewControllers:@[self.viewController]];
  self.window.rootViewController = self.navigationController;
  [self.window makeKeyAndVisible];

这样做会导致崩溃

[self.window makeKeyAndVisible];

但痕迹完全一样。

我也通过更改尝试使用香草 ViewController 进行此操作

[self.navigationController setViewControllers:@[[[UIViewController alloc] init]]];

同样的结果...

原帖:

我遇到了一个我很难理解的崩溃 - 这是 lldb 跟踪:注意 2147483648

的索引
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM removeObjectAtIndex:]: index 2147483648 beyond bounds [0 .. 2]'
*** First throw call stack:
(
0   CoreFoundation                      0x035eaa94 __exceptionPreprocess + 180
1   libobjc.A.dylib                     0x03084e02 objc_exception_throw + 50
2   CoreFoundation                      0x034f92ed -[__NSArrayM removeObjectAtIndex:] + 445
3   UIKit                               0x018c20b2 -[UIView(Hierarchy) bringSubviewToFront:] + 260
4   UIKit                               0x0193daeb -[UINavigationBar layoutSubviews] + 3692
5   UIKit                               0x018d716b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 813
6   libobjc.A.dylib                     0x03099059 -[NSObject performSelector:withObject:] + 70
7   QuartzCore                          0x0096e60c -[CALayer layoutSublayers] + 144
8   QuartzCore                          0x0096228e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 388
9   QuartzCore                          0x00970b2c -[CALayer(CALayerPrivate) layoutBelowIfNeeded] + 44
10  UIKit                               0x018c4dca -[UIView(Hierarchy) layoutBelowIfNeeded] + 1244
11  UIKit                               0x01a117cf __74-[UINavigationController _positionNavigationBarHidden:edge:initialOffset:]_block_invoke + 36
12  UIKit                               0x018caca6 +[UIView(Animation) performWithoutAnimation:] + 82
13  UIKit                               0x01a1178d -[UINavigationController _positionNavigationBarHidden:edge:initialOffset:] + 922
14  UIKit                               0x01a1194c -[UINavigationController _positionNavigationBarHidden:edge:] + 326
15  UIKit                               0x01a12d5f -[UINavigationController _positionNavigationBarHidden:] + 49
16  UIKit                               0x01a1104a -[UINavigationController setNavigationBar:] + 1224
17  UIKit                               0x01a10a38 -[UINavigationController _navigationBarHiddenByDefault:] + 156
18  UIKit                               0x01a10997 -[UINavigationController navigationBar] + 41
19  UIKit                               0x01a17805 -[UINavigationController loadView] + 230
20  UIKit                               0x019d3338 -[UIViewController loadViewIfRequired] + 138
21  UIKit                               0x019d3cf1 -[UIViewController view] + 35
22  UIKit                               0x01a22226 -[UINavigationController pushViewController:transition:forceImmediate:] + 615
23  UIKit                               0x01a21e27 __54-[UINavigationController pushViewController:animated:]_block_invoke + 351
24  UIKit                               0x01a21c83 -[UINavigationController pushViewController:animated:] + 786
25  UIKit                               0x01a07be2 -[UINavigationController initWithRootViewController:] + 140
26  DELETIA                             0x0012954e -[AppDelegate application:didFinishLaunchingWithOptions:] + 1214

这是一个成熟的应用程序,已经构建和运行了一段时间,但在当前的 XCode 中发生了上述情况。

如您所见,调用了 UINavigationController:initWithRootViewController - 这是代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// deletia - non UIKit code

  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  self.window.tintColor = [UIColor darkGrayColor];

  self.viewController = [[ProjectsViewController alloc] initWithNibName:@"ProjectsViewController_iPhone" bundle:nil];

  self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];

// deletia - but the app crashes on the above line
}

在查看了一些类似的问题后,我尝试了一些事情,并且 答案在这里。

  • 我听说如果基于 View 控制器的状态栏会发生这种情况 在 Info.plist 中外观设置为 YES - 所以我将其设置为 NO / 是的

  • 我听说某些 UIGestureRecognizer 会导致问题 - 所以我检查了 XIB 并确保没有影响此视图控制器。

  • 我听说如果根视图控制器没有完全初始化 这可能有问题 - 所以我推迟了对 UINavigationController 1 秒

  • 我误信了 ProjectsViewController - 所以我用它代替了 一个香草 UIViewController 因此:

    self.navigationController = [[UINavigationController alloc] initWithRootViewController:[[UIViewController alloc] init]];
    

非常感谢任何见解;无论是可能导致问题的原因,还是可能对问题有所启发的调试技术。

【问题讨论】:

  • 我注意到 2147483648 是 0x80000000 但不明白。您是否向 UINavigationBar 添加了系统尝试删除的任何内容?也许尝试在导航栏上添加一个按钮,看看是否有任何变化?
  • @ishahak 你的意思是在 xib 中吗?
  • 是的,因为崩溃堆栈显示 UINavigationBar 正在运行它的“layoutSubviews”处理程序 -> BringSubviewToFront -> removeObjectAtIndex。不知道它为什么要从数组中删除某些东西,但我会尝试用更多元素填充这个数组(导航栏的子视图)。如果不太复杂,我个人会尝试重新创建 XIB。
  • hmm - XIB 真的很简单(根本没有导航组件) - 我可以回溯到旧版本(大约一年前 tbh)并执行它 ok - 我检查并没有更改即 XIB(或在 App Delegate 中,而不是在上述所有操作之后添加 crashalytics)
  • 看起来有些东西(也许在代码中)要求将子视图放在前面。我想这是在其父视图中询问视图的索引并要求将其删除,以便它可以在最后添加它。搜索必须返回 NSNotFound,它是 2147483648(最大整数值),然后它使用它来进行删除。看起来您应该专注于 NavigationBar 和与栏相关的任何代码。它似乎在位置导航栏隐藏。默认是隐藏的吗?

标签: ios objective-c debugging uikit


【解决方案1】:

我认为您关注的是错误的代码。正如您所提到的,它在[self.window makeKeyAndVisible] 行上崩溃,但是,可能导致崩溃的原因是该行导致ProjectsViewControllerUINavigationController 对象加载并呈现给用户。查看您发布的崩溃日志,这是您应该调查的部分:

2   CoreFoundation                      0x034f92ed -[__NSArrayM removeObjectAtIndex:] + 445
3   UIKit                               0x018c20b2 -[UIView(Hierarchy) bringSubviewToFront:] + 260
4   UIKit                               0x0193daeb -[UINavigationBar layoutSubviews] + 3692

您可以在此处看到 iOS 尝试布局 UINavigationBar 的子视图,然后删除索引可能为 NSNotFound 的对象(这将导致 NSIntegerMax,与您在崩溃日志中提到的索引匹配) .

为了进一步研究崩溃,我建议按照以下步骤操作:

  1. 用没有自定义 .xib 文件的 UIViewController 实例替换您的 ProjectsViewController 实例。 (我在您的原始帖子中读到您已经尝试过此操作,但我仍然建议您将其作为第一步,以便在完全解决崩溃的过程中消除其他问题(如果有多个问题)。请务必确保此时您没有使用 .xib 文件,因为这可能是导致崩溃的原因(如果存在任何错误链接的出口或类似问题)。
  2. 您的导航栏是否显示了任何项目?再次,我注意到您在 cmets 中提到它没有,但我会仔细检查是否有一个代码部分,其中项目在某些条件下添加到栏,如果不满足则删除。如果尝试从无效索引中删除项目,这可能会导致崩溃。
  3. 尝试在代码中搜索专门调用removeObjectAtIndex 或任何其他可能在初始视图控制器的加载和显示过程中调用的NSMutableArray 相关调用的位置。在这些地方添加断点,看看在初始加载过程中是否到达。如果是这样,请尝试在那里添加一个测试,以确保您尝试从中删除对象的索引大于或等于零并且小于数组的大小。这样,如果您尝试访问的索引是NSNotFound,它至少不会导致应用程序崩溃(无论当前崩溃如何,这是一个很好的做法)。如果这最终解决了问题,那么您可以进一步调查问题并尝试了解为什么 NSNotFound 实际上被作为索引接收并从逻辑上解决问题。

我希望这些步骤之一可以帮助您识别和解决您的问题。祝你好运!

【讨论】:

  • 我同意导航栏应该是我的第一个攻击点,但不幸的是,我看不到我试图更改/添加/删除它的任何方式,或者确实与一个导航栏……
  • Welp,赏金已经结束,似乎没有新的答案。猜猜这个得到它!
【解决方案2】:

UINavigationController 是一个堆栈,您只能在其中推送和弹出 UIViewController。您应该在启动导航控制器时传递UIViewController。但是如果你不知道 RootViewController 那么你可以这样做。

self.viewController = [[ProjectsViewController alloc] initWithNibName:@"ProjectsViewController_iPhone" bundle:nil];
  self.navigationController = [[UINavigationController alloc] init];
  [self.navigationController pushViewController:self.viewController animated:NO];
  self.window.rootViewController = self.navigationController;
  [self.window makeKeyAndVisible];

【讨论】:

  • 这将如何导致崩溃?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-03
  • 2014-04-10
  • 1970-01-01
  • 2019-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多