amirdew/CollectionViewPagingLayout

swiftui ensure currentPage is greater than or equal to zero to prevent Index out of range error

jlynch1 opened this issue · 1 comments

in swiftui code for latest release 1.0.3 through swiftpm

\Swift/ContiguousArrayBuffer.swift:580: Fatal error: Index out of range
2022-03-14 12:07:46.711332-0300 app[5708:2182143] Swift/ContiguousArrayBuffer.swift:580: Fatal error: Index out of range
error: Execution was interrupted, reason: EXC_BREAKPOINT (code=1, subcode=0x186695a64).
The process has been returned to the state before expression evaluation.

This issue shows up in shutdown/init sequence where a viewcontroller is dismissed and afterwards the model data and the selection (stored in a class passed as a parameter to the viewcontroller) are reset for use the next time the viewcontroller is invoked. Not sure how currentPage ends up being -1, but it would be prudent to have a check for >= 0 also. Not sure if its down to a retain cycle that the data model is still connected to ScalePageView - I was expecting that it would be deinitialised fully on dismissing the viewcontroller. It is a viewcontroller with only a UIHostingController with a SwiftUI view with ScalePageView inside it.

 private func setupOnCurrentPageChanged(_ viewController: ViewController) {
        viewController.onCurrentPageChanged = { [data, selection] in
            guard $0 < data.count else { return }
            selection?.wrappedValue = data[$0].id
        }
    }

  public func onCurrentPageChanged(layout: CollectionViewPagingLayout, currentPage: Int) {
        onCurrentPageChanged?(currentPage)
    }
(lldb) print $0
(Int) $R0 = -1
(lldb) print data
([Info]) $R1 = 0 values {}
(lldb) print selection
(SwiftUI.Binding<String?>?) $R2 = some {
  transaction = {
    plist = {
      elements = nil
    }
  }
  location = 0x0000000281a1ad00 {
    SwiftUI.AnyLocationBase = {}
  }
  _value = nil
}
Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x00000001853f21a8
Exception Note:  EXC_CORPSE_NOTIFY
Termination Reason: SIGNAL 5 Trace/BPT trap: 5
Terminating Process: exc handler [330]

Triggered by Thread:  0


Kernel Triage:
VM - pmap_enter failed with resource shortage
VM - pmap_enter failed with resource shortage


Thread 0 name:
Thread 0 Crashed:
0   libswiftCore.dylib              0x00000001853f21a8 closure #1 in closure #1 in closure #1 in _assertionFailure(_:_:file:line:flags:) + 300 (AssertCommon.swift:96)
1   libswiftCore.dylib              0x00000001853f1f48 closure #1 in closure #1 in _assertionFailure(_:_:file:line:flags:) + 220 (AssertCommon.swift:89)
2   libswiftCore.dylib              0x00000001853f183c _assertionFailure(_:_:file:line:flags:) + 232 (AssertCommon.swift:85)
3   libswiftCore.dylib              0x00000001853ce594 _ArrayBuffer._checkInoutAndNativeTypeCheckedBounds(_:wasNativeTypeChecked:) + 280 (ContiguousArrayBuffer.swift:580)
4   libswiftCore.dylib              0x00000001853d3380 Array.subscript.getter + 88 (Array.swift:383)
5   app                          0x0000000100afacf8 closure #1 in PagingCollectionViewControllerBuilder.setupOnCurrentPageChanged(_:) + 372 (PagingCollectionViewControllerBuilder.swift:77)
6   app                          0x0000000100af9b40 specialized PagingCollectionViewController.onCurrentPageChanged(layout:currentPage:) + 60 (PagingCollectionViewController.swift:53)
7   app                          0x0000000100ae2590 currentPage.didset + 84 (CollectionViewPagingLayout.swift:62)
8   app                          0x0000000100ae2590 currentPage.set + 88 (CollectionViewPagingLayout.swift:0)
9   app                          0x0000000100ae2590 CollectionViewPagingLayout.updateCurrentPageIfNeeded() + 468 (CollectionViewPagingLayout.swift:245)
10  app                          0x0000000100ae2308 CollectionViewPagingLayout.invalidateLayout() + 72 (CollectionViewPagingLayout.swift:225)
11  app                          0x0000000100ae23ac @objc CollectionViewPagingLayout.invalidateLayout() + 28 (<compiler-generated>:0)
12  app                          0x0000000100ae4db4 partial apply for closure #1 in closure #1 in CollectionViewPagingLayout.invalidateLayoutInBatchUpdate(invalidateOffset:) + 68 (CollectionViewPagingLayout.swift:134)
13  app                          0x0000000100ae4df0 partial apply for thunk for @callee_guaranteed () -> () + 20 (<compiler-generated>:0)
14  app                          0x0000000100ae20f8 thunk for @escaping @callee_guaranteed () -> () + 20 (<compiler-generated>:0)
15  UIKitCore                       0x0000000183183328 -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:animator:animationHandler:] + 420 (UICollectionView.m:10402)
16  app                          0x0000000100ae20a8 closure #1 in CollectionViewPagingLayout.invalidateLayoutInBatchUpdate(invalidateOffset:) + 572 (CollectionViewPagingLayout.swift:133)
17  app                          0x0000000100ae0838 thunk for @escaping @callee_guaranteed () -> () + 20 (<compiler-generated>:0)
18  libdispatch.dylib               0x0000000180671924 _dispatch_call_block_and_release + 32 (init.c:1517)
19  libdispatch.dylib               0x0000000180673670 _dispatch_client_callout + 20 (object.m:560)
20  libdispatch.dylib               0x0000000180681b70 _dispatch_main_queue_callback_4CF + 944 (inline_internal.h:2601)
21  CoreFoundation                  0x00000001809b9d84 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16 (CFRunLoop.c:1795)
22  CoreFoundation                  0x0000000180973f5c __CFRunLoopRun + 2540 (CFRunLoop.c:3144)
23  CoreFoundation                  0x0000000180987468 CFRunLoopRunSpecific + 600 (CFRunLoop.c:3268)
24  GraphicsServices                0x000000019c52b38c GSEventRunModal + 164 (GSEvent.c:2200)
25  UIKitCore                       0x000000018332a5d0 -[UIApplication _run] + 1100 (UIApplication.m:3493)
26  UIKitCore                       0x00000001830a8f74 UIApplicationMain + 364 (UIApplication.m:5047)
27  app                          0x000000010094c4c0 main + 68 (CircleButton.swift:22)
28  dyld                            0x0000000100eb5aa4 start + 520 (dyldMain.cpp:879)