0

I am unable to reproduce the issue. One of my user sent the crash log and the issue seem to point to NSUserDefault setBool:forKey method.

Not working for user Device iPhone 6, iOS version: 12.4.4. For now only one user contacted due to the App crash.

Working on the devices I tested. (iPad Air 2, iPhone XS, iOS version 13.3)

This is the method I am using.

- (void) saveToDefaults:(NSString*)productId
{        
    [ [ NSUserDefaults standardUserDefaults ] setBool:YES forKey:productId ];
    [ [ NSUserDefaults standardUserDefaults ] synchronize];
}

Crash dump

Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x000000020eae32e4
Termination Signal: Trace/BPT trap: 5
Termination Reason: Namespace SIGNAL, Code 0x5
Terminating Process: exc handler [15897]
Triggered by Thread:  0

Thread 0 name:
Thread 0 Crashed:
0   CoreFoundation                  0x000000020eae32e4 CFHash + 372 (CFRuntime.c:1147)
1   CoreFoundation                  0x000000020eb75a38 CFBasicHashFindBucket + 224 (CFBasicHash.c:465)
2   CoreFoundation                  0x000000020eb75a38 CFBasicHashFindBucket + 224 (CFBasicHash.c:465)
3   CoreFoundation                  0x000000020eaada40 CFDictionaryGetValue + 120 (CFDictionary.c:413)
4   CoreFoundation                  0x000000020eaca938 -[CFPrefsSearchListSource alreadylocked_setPrecopiedValues:forKeys:count:from:] + 496 (CFPrefsSearchListSource.m:632)
5   CoreFoundation                  0x000000020eb81230 -[CFPrefsSource setValues:forKeys:count:copyValues:removeValuesForKeys:count:from:] + 372 (CFPrefsSource.m:742)
6   CoreFoundation                  0x000000020eb81524 -[CFPrefsSource setValues:forKeys:count:copyValues:from:] + 36 (CFPrefsSource.m:766)
7   CoreFoundation                  0x000000020ea3e3dc -[CFPrefsSource setValue:forKey:from:] + 64 (CFPrefsSource.m:772)
8   CoreFoundation                  0x000000020eaccc54 __108-[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurat... + 272 (CFPrefsSearchListSource.m:1553)
9   CoreFoundation                  0x000000020eacc698 normalizeQuintuplet + 340 (CFPrefsSearchListSource.m:63)
10  CoreFoundation                  0x000000020ea3ad64 -[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationUR... + 108 (CFPrefsSearchListSource.m:1433)
11  CoreFoundation                  0x000000020ea3dd7c -[_CFXPreferences setValue:forKey:appIdentifier:container:configurationURL:] + 92 (CFXPreferences.m:759)
12  CoreFoundation                  0x000000020eb8b360 _CFPreferencesSetAppValueWithContainerAndConfiguration + 132 (CFXPreferences.m:1867)
13  Foundation                      0x000000020f4eb3b8 -[NSUserDefaults(NSUserDefaults) setObject:forKey:] + 64 (NSUserDefaults.m:228)
14  MyApp                           0x00000001004adb14 -[IAPHelper saveToDefaults:] + 88 (IAPHelper.m:206)
15  MyApp                           0x00000001004adc3c -[IAPHelper restoreTransaction:] + 224 (IAPHelper.m:223)
16  MyApp                           0x00000001004ad928 -[IAPHelper paymentQueue:updatedTransactions:] + 264 (IAPHelper.m:0)
17  libdispatch.dylib               0x000000020e58ca38 _dispatch_call_block_and_release + 24 (init.c:1372)
18  libdispatch.dylib               0x000000020e58d7d4 _dispatch_client_callout + 16 (object.m:511)
19  libdispatch.dylib               0x000000020e53b008 _dispatch_main_queue_callback_4CF$VARIANT$mp + 1068 (inline_internal.h:2441)
20  CoreFoundation                  0x000000020eae0b20 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12 (CFRunLoop.c:1813)
21  CoreFoundation                  0x000000020eadba58 __CFRunLoopRun + 1924 (CFRunLoop.c:3113)
22  CoreFoundation                  0x000000020eadafb4 CFRunLoopRunSpecific + 436 (CFRunLoop.c:3247)
23  GraphicsServices                0x0000000210cdc79c GSEventRunModal + 104 (GSEvent.c:2245)
24  UIKitCore                       0x000000023b33cc38 UIApplicationMain + 212 (UIApplication.m:4353)
25  MyApp                           0x00000001004e9cec main + 88 (main.m:14)
26  libdyld.dylib                   0x000000020e59e8e0 start + 4

How can I handle this exception to avoid this problem in the future.

Jafar Mohammed
  • 103
  • 1
  • 12
  • If you're targetting min iOS 12.0, you can remove `synchronize` call. https://stackoverflow.com/a/57218546/5329717 Not sure if it will help in your case though, but worth a try if applicable. You could also conditionally call it only for iOS < 12 – Kamil.S Jan 22 '20 at 09:47
  • Thanks for responding. My target iOS is from 11.2, guess I cannot use it for now. – Jafar Mohammed Jan 23 '20 at 05:37

1 Answers1

2

Following up comments discussion and this answer indicating synchronize is not required (as per Apple's iOS 12 release notes):

My target iOS is from 11.2, guess I cannot use it for now.

Yes you can.

Wrapping up my suggestion in comment :

if (@available(iOS 12, *)) {
   // don't need to synchronize
} else {
   [NSUserDefaults.standardUserDefaults synchronize];
}

The above code @available Swift-like syntax assumes you're using Xcode 9 or higher. Which given the targeted iOS is pretty much a must.

Just to make myself 100% clear I cannot guarantee this will solve this particular crash issue, but it is worth trying as a very low effort fix candidate.

ricardopereira
  • 11,118
  • 5
  • 63
  • 81
Kamil.S
  • 5,205
  • 2
  • 22
  • 51