调试UIKit崩溃[UINavigationController initWithRootViewController]

Dam*_*amo 21 debugging objective-c uikit ios

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];
Run Code Online (Sandbox Code Playgroud)

这样做现在崩溃了

[self.window makeKeyAndVisible];
Run Code Online (Sandbox Code Playgroud)

但痕迹完全一样.

我也通过更改使用香草ViewController尝试了这个

[self.navigationController setViewControllers:@[[[UIViewController alloc] init]]];
Run Code Online (Sandbox Code Playgroud)

结果相同......

原帖:

我遇到了一个我很难理解的崩溃 - 这是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
Run Code Online (Sandbox Code Playgroud)

这是一个成熟的应用程序,已经建立并运行了一段时间,但在当前的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
}
Run Code Online (Sandbox Code Playgroud)

在SO上查看了一些类似的问题和答案后,我尝试了一些方法.

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

  • 我听说有些UIGestureRecognizers会导致问题 - 所以我检查了XIB并确保没有影响这个视图控制器.

  • 我听说,如果根视图控制器没有完全初始化可能会造成问题 - 所以我已经延迟1秒的调用的UINavigationController

  • 我已经错信任ProjectsViewController - 所以我代替它的香草的UIViewController这样的:

    self.navigationController = [[UINavigationController alloc] initWithRootViewController:[[UIViewController alloc] init]];
    
    Run Code Online (Sandbox Code Playgroud)

任何见解都非常赞赏; 无论是在什么可能导致该问题或技术调试可能抛出对这个问题的一些情况.

Ron*_*zen 6

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

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

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

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

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

我希望其中一个步骤有助于识别和解决您的问题.祝好运!