0

I have a weird crash while using NSUserDefaults and I'm unable to reproduce this myself but I get this reported from Crashlytics from one of user's devices.

Exception: EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000000

Crashed: com.apple.NSURLConnectionLoader
0  libobjc.A.dylib                0x22613a76 objc_msgSend + 21
1  CoreFoundation                 0x22e97a58 __CFBasicHashDrain + 448
2  CoreFoundation                 0x22d77795 CFRelease + 424
3  CoreFoundation                 0x22e979e8 __CFBasicHashDrain + 336
4  CoreFoundation                 0x22d77795 CFRelease + 424
5  CoreFoundation                 0x22e26b8f -[CFPrefsSearchListSource alreadylocked_copyDictionary] + 906
6  CoreFoundation                 0x22e258ad -[CFPrefsSearchListSource alreadylocked_copyValueForKey:] + 52
7  CoreFoundation                 0x22e9f6bb -[CFPrefsSource copyValueForKey:] + 50
8  CoreFoundation                 0x22ea1e3d ___CFPreferencesCopyAppValueWithContainerAndConfiguration_block_invoke + 28
9  CoreFoundation                 0x22e23969 __95+[CFPrefsSearchListSource withSearchListForIdentifier:container:cloudConfigurationURL:perform:]_block_invoke + 400
10 CoreFoundation                 0x22e2377f normalizeQuintuplet + 354
11 CoreFoundation                 0x22e23617 +[CFPrefsSearchListSource withSearchListForIdentifier:container:cloudConfigurationURL:perform:] + 106
12 CoreFoundation                 0x22ea1dc9 _CFPreferencesCopyAppValueWithContainerAndConfiguration + 248
13 Foundation                     0x235c436f -[NSUserDefaults(NSUserDefaults) objectForKey:] + 46
14 MyApp                       0x12ddc49 -[MyStore getConfigValue:] (MyStore.m:43)

The code in MyStore is as below:

- (id) getConfigValue:(NSString *)key {
    if (key) {
        return [[NSUserDefaults standardUserDefaults] valueForKey:key];
    }
    return nil;
}

And I call this method as below

NSString *userId = [storeObj getConfigValue:@"UserID"];

Now, this value may not be set in some cases but that should just return nil. I'm unable to find any valid reason for this to crash.

Thanks for help.

Ravi
  • 523
  • 8
  • 23
  • 1
    Stop using `valueForKey` unless you **really** need KVC – vadian Jun 01 '16 at 10:35
  • Yeah. I will change over to objectForKey method based on various other answers I was reading on SO. But I'm not sure if that will resolve the crash because valueForKey method did just call the objectForKey method as per the stack trace. – Ravi Jun 01 '16 at 10:38
  • `valueForKey` could return an array of objects rather than an expected single object. – vadian Jun 01 '16 at 11:02
  • I did change my code to objectForKey on your suggestion. But from the above stacktrace, valueForKey method never returned but crashed off. Since I'm unable to reproduce the crash myself I'm thinking it would help if I know the reason for the crash. – Ravi Jun 01 '16 at 11:08
  • It's just a hint to reliably exclude that KVC issue. – vadian Jun 01 '16 at 11:10
  • thanks @vadian I have changed to objectForKey as that will suffice my purpose. Hope the crash gets resolved. – Ravi Jun 01 '16 at 11:31
  • I have replaced valueForKey with objectForKey and I still end up with this issue. – Ravi Aug 05 '16 at 09:37
  • @Ravi did you find the cause/solution for this issue? – user3247895 Jan 16 '20 at 19:19
  • not really, I was mostly confident crash is not due to this bit of code atleast.. so had to move on to more important things.. – Ravi Jan 17 '20 at 02:20

0 Answers0