heap / heap-ios-sdk

Heap iOS SDK
4 stars 2 forks source link

Crashing issue when Heap SDK installed using SPM #8

Closed NurLight19 closed 2 years ago

NurLight19 commented 2 years ago

I'm facing a crashing issue after started using Heap SDK via Swift Package Manager (used Cocoapods before). I couldn't find the reason for it. It is happening with all iOS versions and devices randomly. Could you please help me to reduce this crash?

Heap SDK: 8.1.0 Device: iPhone 13 Pro Max iOS version: 15.1.1

Crashed: com.Heap.serialState (QOS: BACKGROUND)
0  libobjc.A.dylib                0x3af8 object_getClass + 48
1  Foundation                     0x41bac _NSKeyValueObservationInfoGetObservances + 264
2  Foundation                     0x43198 NSKeyValueWillChange + 176
3  Foundation                     0x2bea0 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKeys:count:maybeOldValuesDict:maybeNewValuesDict:usingBlock:] + 428
4  Foundation                     0x4a708 -[NSObject(NSKeyValueObservingPrivate) _notifyObserversOfChangeFromValuesForKeys:toValuesForKeys:] + 824
5  CoreFoundation                 0x60508 -[CFPrefsSource forEachObserver:] + 328
6  CoreFoundation                 0xfae34 -[CFPrefsSource _notifyObserversOfChangeFromValuesForKeys:toValuesForKeys:] + 112
7  CoreFoundation                 0xd5f1c ___CFPrefsDeliverPendingKVONotificationsGuts_block_invoke + 440
8  CoreFoundation                 0x2be6c __CFDictionaryApplyFunction_block_invoke + 28
9  CoreFoundation                 0x29734 CFBasicHashApply + 144
10 CoreFoundation                 0x2b648 CFDictionaryApplyFunction + 328
11 CoreFoundation                 0xc1184 _CFPrefsDeliverPendingKVONotificationsGuts + 300
12 CoreFoundation                 0x286d4 -[_CFXPreferences _deliverPendingKVONotifications] + 96
13 CoreFoundation                 0x57324 __108-[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:]_block_invoke + 432
14 CoreFoundation                 0x2f328 normalizeQuintuplet + 356
15 CoreFoundation                 0x283ac -[_CFXPreferences withSearchListForIdentifier:container:cloudConfigurationURL:perform:] + 152
16 CoreFoundation                 0xd9050 -[_CFXPreferences setValue:forKey:appIdentifier:container:configurationURL:] + 128
17 CoreFoundation                 0x5c7fc _CFPreferencesSetAppValueWithContainerAndConfiguration + 136
18 Foundation                     0x3ad24 -[NSUserDefaults(NSUserDefaults) setObject:forKey:] + 84
19 Heap                           0x26cd0 _hidden#2113_ + 49 (__hidden#2130_:49)
20 Heap                           0x26074 _hidden#2057_ + 255 (__hidden#2072_:255)
21 Heap                           0x26c04 _hidden#2112_ + 52 (__hidden#2130_:52)
22 Heap                           0x269f4 _hidden#2109_ + 26 (__hidden#2130_:26)
23 Heap                           0x7558 _hidden#107_ + 403 (__hidden#615_:403)
24 Heap                           0x15050 _hidden#1008_ + 29 (__hidden#1016_:29)
25 Foundation                     0x50ad0 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 24
26 Foundation                     0x62c58 -[NSBlockOperation main] + 104
27 Foundation                     0x3b6f4 __NSOPERATION_IS_INVOKING_MAIN__ + 24
28 Foundation                     0x4c570 -[NSOperation start] + 804
29 Foundation                     0x4fb18 __NSOPERATIONQUEUE_IS_STARTING_AN_OPERATION__ + 24
30 Foundation                     0x5dbb0 __NSOQSchedule_f + 184
31 libdispatch.dylib              0x2914 _dispatch_call_block_and_release + 32
32 libdispatch.dylib              0x4660 _dispatch_client_callout + 20
33 libdispatch.dylib              0x7b34 _dispatch_continuation_pop + 504
34 libdispatch.dylib              0x7190 _dispatch_async_redirect_invoke + 596
35 libdispatch.dylib              0x15dd0 _dispatch_root_queue_drain + 396
36 libdispatch.dylib              0x165f8 _dispatch_worker_thread2 + 164
37 libsystem_pthread.dylib        0x10b8 _pthread_wqthread + 228
38 libsystem_pthread.dylib        0xe94 start_wqthread + 8
bnickel commented 2 years ago

Hi Nuriddin,

Does your app use google-toolbox-fox-mac or a library that depends on it like Firebase/Performance? If so, you may be running into this bug and the solution would be updating those dependencies.

The linked bug happens when the following things happen:

  1. The app adds a Key Value Observing observer to [NSUserDefaults standardUserDefaults].
  2. The app removes the observer during dealloc/deinit.
  3. A background thread writes to NSUserDefaults.

This creates a race condition where an element in an array can get deallocated while it's being iterated over, causing a crash.

NurLight19 commented 2 years ago

Hi Brian, Thanks for the answer, looks like you are giving the correct path to fix the issue 👍