Crash when contentOffset is being updated

441 Views Asked by At
let bottomOffset = bottomView.contentOffset.y
let currentOffset = overlayScrollView?.contentOffset.y ?? CGFloat(0)
let totalOffset = (bottomOffset + currentOffset) > 0 ? (bottomOffset + currentOffset)  : 0
bottomView.contentOffset.y = 0
overlayScrollView?.contentOffset.y = totalOffset

I am facing several crashes of type EXC_BAD_ACCESS KERN_PROTECTION_FAILURE in app on this line

overlayScrollView?.contentOffset.y = totalOffset

Initially, I thought this is happening because KVO is not being handled properly, that might be message is being sent to deallocated object and observer is not removed. Moved to new KVO keyPathBased APIs which removed overhead of removing observer. But still this issue is happening. This issue is not reproducible at all on my end.

0  QuartzCore                     0x1a42bb938 CA::Layer::writable_state(CA::Transaction*) + 490
1  QuartzCore                     0x1a42bba74 CA::Layer::end_change(CA::Transaction*, unsigned int, objc_object*, objc_object*) + 32
2  QuartzCore                     0x1a42c4a04 CA::Layer::set_position(CA::Vec2<double> const&, bool) + 312
3  QuartzCore                     0x1a42b2dbc -[CALayer setPosition:] + 48
4  QuartzCore                     0x1a42b3450 -[CALayer setFrame:] + 424
5  UIKitCore                      0x1a3e28df0 -[UIView(Geometry) setFrame:] + 472
6  UIKitCore                      0x1a3dcf4b4 -[UIScrollView _layoutHorizontalScrollIndicatorWithBounds:effectiveInset:contentOffset:fraction:additionalInset:cornerAdjust:showing:recalcSize:verticalIndicatorFrame:] + 1320
7  UIKitCore                      0x1a3dce89c -[UIScrollView _adjustScrollerIndicators:alwaysShowingThem:] + 1288
8  UIKitCore                      0x1a3dbabfc -[UIScrollView setContentOffset:] + 1208
9  UIKitCore                      0x1a3b46ed4 -[UITableView setContentOffset:] + 284
10 Foundation                     0x1a236dbd8 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKeys:count:maybeOldValuesDict:maybeNewValuesDict:usingBlock:] + 560
11 Foundation                     0x1a22b2600 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKey:key:key:usingBlock:] + 68
12 Foundation                     0x1a2369614 _NSSetPointValueAndNotify + 300
13 App                            0x10488393c partial apply for closure #2 in PageBaseViewController.updatePanView(view:index:) + 540 (PageBaseViewController.swift:540)
14 App                            0x1048822d8 partial apply for thunk for @escaping @callee_guaranteed (@guaranteed ZScrollView, @in_guaranteed NSKeyValueObservedChange<CGPoint>) -> () + 4369375960 (<compiler-generated>:4369375960)
15 libswiftFoundation.dylib       0x1a48d6a50 closure #1 in _KeyValueCodingAndObserving.observe<A>(_:options:changeHandler:) + 320
16 libswiftFoundation.dylib       0x1a48d9cc4 specialized NSKeyValueObservation.Helper._swizzle_me_observeValue(forKeyPath:of:change:context:) + 1268
17 libswiftFoundation.dylib       0x1a48d6494 @objc NSKeyValueObservation.Helper._swizzle_me_observeValue(forKeyPath:of:change:context:) + 192
18 Foundation                     0x1a236bd70 NSKeyValueNotifyObserver + 288
19 Foundation                     0x1a236e188 NSKeyValueDidChange + 324
20 Foundation                     0x1a236dc14 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKeys:count:maybeOldValuesDict:maybeNewValuesDict:usingBlock:] + 620
21 Foundation                     0x1a22b2600 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKey:key:key:usingBlock:] + 68
22 Foundation                     0x1a2369614 _NSSetPointValueAndNotify + 300
23 UIKitCore                      0x1a3b386b0 -[UITableView _restoreOrAdjustContentOffsetWithRowCount:initialContentInsetTop:] + 352
24 UIKitCore                      0x1a3b27a50 -[UITableView _updateVisibleCellsNow:] + 3552
25 UIKitCore                      0x1a3b27a78 -[UITableView _updateVisibleCellsNow:] + 3592
26 UIKitCore                      0x1a3b27a78 -[UITableView _updateVisibleCellsNow:] + 3592
27 UIKitCore                      0x1a3b27a78 -[UITableView _updateVisibleCellsNow:] + 3592
28 UIKitCore                      0x1a3b27a78 -[UITableView _updateVisibleCellsNow:] + 3592
29 UIKitCore                      0x1a3b27a78 -[UITableView _updateVisibleCellsNow:] + 3592
30 UIKitCore                      0x1a3b27a78 -[UITableView _updateVisibleCellsNow:] + 3592
31 UIKitCore                      0x1a3b27a78 -[UITableView _updateVisibleCellsNow:] + 3592
32 UIKitCore                      0x1a3b27a78 -[UITableView _updateVisibleCellsNow:] + 3592
33 UIKitCore                      0x1a3b27a78 -[UITableView _updateVisibleCellsNow:] + 3592
34 UIKitCore                      0x1a3b27a78 -[UITableView _updateVisibleCellsNow:] + 3592
35 UIKitCore                      0x1a3b44938 -[UITableView layoutSubviews] + 356
36 UIKitCore                      0x1a3e44910 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2544
37 QuartzCore                     0x1a42b96dc -[CALayer layoutSublayers] + 288
38 QuartzCore                     0x1a42bfe64 CA::Layer::layout_if_needed(CA::Transaction*) + 544
39 QuartzCore                     0x1a42cb424 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 140
40 QuartzCore                     0x1a4213c20 CA::Context::commit_transaction(CA::Transaction*, double, double*) + 500
41 QuartzCore                     0x1a423e970 CA::Transaction::commit() + 668
42 UIKitCore                      0x1a39752c0 _afterCACommitHandler + 140
43 CoreFoundation                 0x1a102d418 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
44 CoreFoundation                 0x1a1027a48 __CFRunLoopDoObservers + 588
45 CoreFoundation                 0x1a1027fe4 __CFRunLoopRun + 1012
46 CoreFoundation                 0x1a10276a8 CFRunLoopRunSpecific + 572
47 GraphicsServices               0x1b76d7570 GSEventRunModal + 160
48 UIKitCore                      0x1a3945370 -[UIApplication _run] + 1052
49 UIKitCore                      0x1a394a8ec UIApplicationMain + 164
50 App                            0x10460b944 main + 15 (main.m:15)
51 libdyld.dylib                  0x1a0d06140 start + 4
0

There are 0 best solutions below