iOS 13.4 Beta 4 - crash

I have this crash reported by Crashlytics, on iOS 13.4, beta 4:

"Fatal Exception: NSInvalidArgumentExceptionindex out of bounds for arranged subview: index = 9 expected to be less than or equal to 8"


Somehow it is triggered by UIAlertController, when I press UIAlertActions, but I also have it on views that contain pickers.


On the console I have this:


"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'index out of bounds for arranged subview: index = 9 expected to be less than or equal to 8'

*** First throw call stack:

(0x1aa549828 0x1aa26bbcc 0x1aa440208 0x1ae97ae38 0x1adc99ce8 0x1adc9c564 0x1ae993210 ....

libc++abi.dylib: terminating with uncaught exception of type NSException "


I have added a breakpoint exception, but it does not help much.


On previous iOS versions, everything was ok.

Replies

Hi, I am encountering a similar problem, in my case it's triggered when closing a view controller and setting rootViewController on the UIWindow.

Were you able to find the cause of the issue ?

I have same crash. In my case, the UIToolbar is crashing the app, laying out its internal items.


Radar # FB7654080


iOS 13.4 UIToolbar crashes app inside call to _layoutBar() ========


This bug only occurs in iOS 13.4. It does not iOS 13.3 and earlier. Verified in Simulator.


Because this bug makes the app unusable, the app has been Removed from Sale on the App store.


This crash occurs when the user clicks on a Tab button to switch to a different Tab view. If that Tab view has a toolbar, with several operating modes, where its toolbar item sets have to be swapped out by viewWillAppear(), the app will crash.


The crash occurs while UIKit is laying out all the subviews in the upcoming Tab view. This is all 100% Apple code. My own functions are not in the crash callback list.


It is 100% repeatable, but sometimes it takes several attempts. For instance, my app has these 2 tabs (among others): File and Favorites.

If the user has a document open in the Files tab and then switches back and forth to Favorites, then back to File tab, ....

It takes 2 or 3 repeats of tab switching to cause the crash.


It looks like UIKit is holding on to the prior count of UIToolbarItems, which does not match the "actual" number of items. And then it inserts items into an out-of-bounds index. CRASH !


So the fix is simple, UIButtonBar._layoutBar() needs to check the actual upper bound index before the insertion.


*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'index out of bounds for arranged subview: index = 5 expected to be less than or equal to 4'

*** First throw call stack:

(

0 CoreFoundation 0x00007fff23e3dcce __exceptionPreprocess + 350
1 libobjc.A.dylib 0x00007fff50b3b9b2 objc_exception_throw + 48
2 CoreFoundation 0x00007fff23e3db0c +[NSException raise:format:] + 188
3 UIKitCore 0x00007fff4901e07c -[UIStackView insertArrangedSubview:atIndex:] + 161
4 UIKitCore 0x00007fff482123d3 -[_UIButtonBar _layoutBar] + 3461
5 UIKitCore 0x00007fff48215ce8 -[_UIButtonBarStackView updateConstraints] + 48
6 UIKitCore 0x00007fff49038bce -[UIView(AdditionalLayoutSupport) _sendUpdateConstraintsIfNecessaryForSecondPass:] + 489
7 UIKitCore 0x00007fff4903919c -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 1187
8 UIKitCore 0x00007fff49039036 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 829
9 Foundation 0x00007fff25a99334 -[NSISEngine withBehaviors:performModifications:] + 84
10 UIKitCore 0x00007fff4903993e __100-[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededWithViewForVariableChangeNotifications:]_block_invoke + 85
11 UIKitCore 0x00007fff4903814b -[UIView(AdditionalLayoutSupport) _withUnsatisfiableConstraintsLoggingSuspendedIfEngineDelegateExists:] + 104
12 UIKitCore 0x00007fff49039499 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededWithViewForVariableChangeNotifications:] + 154
13 UIKitCore 0x00007fff4903a3b8 -[UIView(AdditionalLayoutSupport) _updateConstraintsAtEngineLevelIfNeededWithViewForVariableChangeNotifications:] + 393
14 UIKitCore 0x00007fff49105e61 -[UIView(Hierarchy) layoutBelowIfNeeded] + 1334
15 UIKitCore 0x00007fff4821b76e -[_UIButtonBarButtonVisualProviderIOS _configureImageOrTitleFromBarButtonItem:compact:] + 977
16 UIKitCore 0x00007fff4821e903 -[_UIButtonBarButtonVisualProviderIOS configureButton:fromBarButtonItem:] + 397
17 UIKitCore 0x00007fff4821c7eb -[_UIButtonBarButtonVisualProviderIOS configureButton:withAppearanceDelegate:fromBarItem:] + 114
18 UIKitCore 0x00007fff482172d9 -[_UIButtonBarButton _configureFromBarItem:appearanceDelegate:isBackButton:] + 83
19 UIKitCore 0x00007fff48216e75 -[_UIButtonBarButton configureFromBarItem:withAppearanceDelegate:] + 53
20 UIKitCore 0x00007fff48213417 -[_UIButtonBar _updatedViewForBarButtonItem:withView:] + 456
21 UIKitCore 0x00007fff48211614 __32-[_UIButtonBar _newGroupLayout:]_block_invoke + 82
22 UIKitCore 0x00007fff48221066 -[_UIButtonBarItemLayout _updateItemView] + 53
23 UIKitCore 0x00007fff48221647 -[_UIButtonBarItemLayout _configure] + 39
24 UIKitCore 0x00007fff48220103 -[_UIButtonBarLayout configure] + 51
25 UIKitCore 0x00007fff48220266 -[_UIButtonBarLayout addLayoutViews:] + 49
26 UIKitCore 0x00007fff48223731 -[_UIButtonBarItemGroupLayout _iterateConfiguredLayouts:] + 228
27 UIKitCore 0x00007fff4822380d -[_UIButtonBarItemGroupLayout _addLayoutViews:] + 111
28 UIKitCore 0x00007fff482201ca -[_UIButtonBarLayout addLayoutViews:layoutGuides:constraintsToActivate:constraintsToDeactivate:] + 111
29 UIKitCore 0x00007fff48211d36 -[_UIButtonBar _layoutBar] + 1768
30 UIKitCore 0x00007fff48215ce8 -[_UIButtonBarStackView updateConstraints] + 48
31 UIKitCore 0x00007fff49038bce -[UIView(AdditionalLayoutSupport) _sendUpdateConstraintsIfNecessaryForSecondPass:] + 489
32 UIKitCore 0x00007fff4903919c -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 1187
33 UIKitCore 0x00007fff49039036 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 829
34 Foundation 0x00007fff25a99334 -[NSISEngine withBehaviors:performModifications:] + 84
35 UIKitCore 0x00007fff490393b2 -[UIView(AdditionalLayoutSupport) _recursiveUpdateConstraintsIfNeededCollectingViews:forSecondPass:] + 112
36 UIKitCore 0x00007fff49039036 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 829
37 UIKitCore 0x00007fff49039036 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 829
38 Foundation 0x00007fff25a99334 -[NSISEngine withBehaviors:performModifications:] + 84
39 UIKitCore 0x00007fff4903993e __100-[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededWithViewForVariableChangeNotifications:]_block_invoke + 85
40 UIKitCore 0x00007fff4903814b -[UIView(AdditionalLayoutSupport) _withUnsatisfiableConstraintsLoggingSuspendedIfEngineDelegateExists:] + 104
41 UIKitCore 0x00007fff49039499 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededWithViewForVariableChangeNotifications:] + 154
42 UIKitCore 0x00007fff4903a3b8 -[UIView(AdditionalLayoutSupport) _updateConstraintsAtEngineLevelIfNeededWithViewForVariableChangeNotifications:] + 393
43 UIKitCore 0x00007fff49105e61 -[UIView(Hierarchy) layoutBelowIfNeeded] + 1334
44 UIKitCore 0x00007fff484c336d -[UINavigationController _positionNavigationBarHidden:edge:initialOffset:] + 880
45 UIKitCore 0x00007fff484c35f7 -[UINavigationController _positionNavigationBarHidden:edge:] + 394
46 UIKitCore 0x00007fff484cdb68 -[UINavigationController _updateBarsForCurrentInterfaceOrientationAndForceBarLayout:] + 145
47 UIKitCore 0x00007fff485788e4 -[UIViewController viewDidMoveToWindow:shouldAppearOrDisappear:] + 218
48 UIKitCore 0x00007fff484caac2 -[UINavigationController viewDidMoveToWindow:shouldAppearOrDisappear:] + 69
49 UIKitCore 0x00007fff4911110e -[UIView(Internal) _didMoveFromWindow:toWindow:] + 1410
50 UIKitCore 0x00007fff49110e5c -[UIView(Internal) _didMoveFromWindow:toWindow:] + 720
51 UIKitCore 0x00007fff49110e5c -[UIView(Internal) _didMoveFromWindow:toWindow:] + 720
52 UIKitCore 0x00007fff49110e5c -[UIView(Internal) _didMoveFromWindow:toWindow:] + 720
53 UIKitCore 0x00007fff49104789 __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 130
54 UIKitCore 0x00007fff4910468e -[UIView(Hierarchy) _postMovedFromSuperview:] + 800
55 UIKitCore 0x00007fff49113c24 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1718
56 UIKitCore 0x00007fff490d4a45 -[UITransitionView transition:fromView:toView:removeFromView:] + 1373
57 UIKitCore 0x00007fff490d44d0 -[UITransitionView transition:fromView:toView:] + 63
58 UIKitCore 0x00007fff490d4213 -[UITransitionView transition:toView:] + 155
59 UIKitCore 0x00007fff484a8769 -[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] + 1661
60 UIKitCore 0x00007fff484a7828 -[UITabBarController transitionFromViewController:toViewController:] + 59
61 UIKitCore 0x00007fff484a3cfb -[UITabBarController _setSelectedViewController:] + 426
62 UIKitCore 0x00007fff484a3b17 -[UITabBarController setSelectedViewController:] + 109
63 UIKitCore 0x00007fff484a777d -[UITabBarController _tabBarItemClicked:] + 602
64 UIKitCore 0x00007fff48c1a4d5 -[UIApplication sendAction:to:from:forEvent:] + 83
65 UIKitCore 0x00007fff482f0147 -[UITabBar _sendAction:withEvent:] + 557
66 UIKitCore 0x00007fff48c1a4d5 -[UIApplication sendAction:to:from:forEvent:] + 83
67 UIKitCore 0x00007fff485cbc83 -[UIControl sendAction:to:forEvent:] + 223
68 UIKitCore 0x00007fff485cbfcb -[UIControl _sendActionsForEvents:withEvent:] + 396
69 UIKitCore 0x00007fff482f2b7d -[UITabBar _buttonUp:] + 113
70 UIKitCore 0x00007fff48c1a4d5 -[UIApplication sendAction:to:from:forEvent:] + 83
71 UIKitCore 0x00007fff485cbc83 -[UIControl sendAction:to:forEvent:] + 223
72 UIKitCore 0x00007fff485cbfcb -[UIControl _sendActionsForEvents:withEvent:] + 396
73 UIKitCore 0x00007fff485caf3c -[UIControl touchesEnded:withEvent:] + 497
74 UIKitCore 0x00007fff48c55d10 -[UIWindow _sendTouchesForEvent:] + 1359
75 UIKitCore 0x00007fff48c57a95 -[UIWindow sendEvent:] + 4501
76 UIKitCore 0x00007fff48c31ed9 -[UIApplication sendEvent:] + 356
77 UIKitCore 0x00007fff48cbc336 __dispatchPreprocessedEventFromEventQueue + 7328
78 UIKitCore 0x00007fff48cbf502 __handleEventQueueInternal + 6565
79 UIKitCore 0x00007fff48cb606b __handleHIDEventFetcherDrain + 88
80 CoreFoundation 0x00007fff23da1c71 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
81 CoreFoundation 0x00007fff23da1b9c __CFRunLoopDoSource0 + 76
82 CoreFoundation 0x00007fff23da1374 __CFRunLoopDoSources0 + 180
83 CoreFoundation 0x00007fff23d9bf6e __CFRunLoopRun + 974
84 CoreFoundation 0x00007fff23d9b884 CFRunLoopRunSpecific + 404
85 GraphicsServices 0x00007fff38b5ac1a GSEventRunModal + 139
86 UIKitCore 0x00007fff48c19220 UIApplicationMain + 1605
87 Fractical Architect iOS 0x000000010ff4c4b5 main + 85

After investigating, I have determined that in my case the crash was caused by setting duplicate (same instance) UIBarButtonItem items in rightBarButtonItems or leftBarButtonItems (for example for spacing purposes)

Fixed by creating unique instances

@Tiberiu891 pointed the problem. You should be careful when adding items to the toolbar. The same instances added several times cause this strange crash. Here is an example:

NSMutableArray* items = [self.toolbar.items mutableCopy];
[items addObject:self.fixedSpaceButton];
//...
[self.toolbar setItems:items];

The correct way doing this is:

NSMutableArray* items = [self.toolbar.items mutableCopy];
if(![items containsObject:self.fixedSpaceButton]) {
    [items addObject:self.fixedSpaceButton];
}
//...
[self.toolbar setItems:items];