Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

Calling [NSUserDefaults objectForKey:] crashes as it attempts to insert nil object into dictionary

Hi, I work at OneSignal, a third-party SDK, and have the following two crash reports submitted by a client for the same issue. Because the crash originates from our SDK, even if the root cause may be elsewhere, the onus is on our team to try to resolve this crash. Additionally, my information is limited as this is not our own crash report.

I'm trying to figure out why this crash happens and how we can address it within our SDK.

Crash Reported:

Fatal Exception: NSInvalidArgumentException

[NSMutableDictionary __addObject:forKey:]: object cannot be nil

Context:

Our SDK makes a call to [NSUserDefaults(NSUserDefaults) objectForKey:] on the standardUserDefaults and passes in a non-null key. It appears further up the call stack, a null value is being added to a dictionary. I assume this is the innards of how the search list is generated.

Additional Information:

The client states that it seems to happen only once per user and the scale is not extremely high but it is increasing. They are unsure what happened and when. They don't have much information about the devices except it has happened on iOS 18.

Log 1:

0  CoreFoundation                 0x2d5fc __exceptionPreprocess
1  libobjc.A.dylib                0x31244 objc_exception_throw
2  CoreFoundation                 0x15548 -[NSMutableDictionary __addObject:forKey:]
3  CoreFoundation                 0x20850 -[__NSDictionaryM __apply:context:]
4  CoreFoundation                 0x54700 ___CFPrefsDeliverPendingKVONotificationsGuts_block_invoke
5  CoreFoundation                 0x52988 __CFDictionaryApplyFunction_block_invoke
6  CoreFoundation                 0x52524 CFBasicHashApply
7  CoreFoundation                 0x21040 CFDictionaryApplyFunction
8  CoreFoundation                 0x7a6b0 _CFPrefsDeliverPendingKVONotificationsGuts
9  CoreFoundation                 0x777d0 -[_CFXPreferences _deliverPendingKVONotifications]
10 CoreFoundation                 0x776ac __108-[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:]_block_invoke
11 CoreFoundation                 0x5cf24 normalizeQuintuplet
12 CoreFoundation                 0x5cd60 -[_CFXPreferences withSearchListForIdentifier:container:cloudConfigurationURL:perform:]
13 CoreFoundation                 0x5cc60 -[_CFXPreferences copyAppValueForKey:identifier:container:configurationURL:]
14 CoreFoundation                 0x5c8bc _CFPreferencesCopyAppValueWithContainerAndConfiguration
15 Foundation                     0xf0dcc -[NSUserDefaults(NSUserDefaults) objectForKey:]
16 OneSignalCore                  0xc7c8 -[OneSignalUserDefaults keyExists:] + 61 (OneSignalUserDefaults.m:61)
17 OneSignalCore                  0xc8b0 -[OneSignalUserDefaults getSavedBoolForKey:defaultValue:] + 70 (OneSignalUserDefaults.m:70)
18 OneSignalCore                  0xbb10 +[OSPrivacyConsentController requiresUserPrivacyConsent] + 59 (OSPrivacyConsentController.m:59)
19 OneSignalCore                  0xbc30 +[OSPrivacyConsentController shouldLogMissingPrivacyConsentErrorWithMethodName:] + 75 (OSPrivacyConsentController.m:75)
20 OneSignalCore                  0x4418 +[OneSignalConfigManager shouldAwaitAppIdAndLogMissingPrivacyConsentForMethod:] + 50 (OneSignalConfigManager.m:50)
21 OneSignalOSCore                0x537c OSOperationRepo.flushDeltaQueue(inBackground:) + 140 (OSOperationRepo.swift:140)
22 OneSignalOSCore                0x4868 closure #1 in OSOperationRepo.pollFlushQueue() + 84 (OSOperationRepo.swift:84)
23 OneSignalOSCore                0x5078 thunk for @escaping @callee_guaranteed @Sendable () -> ()
24 libdispatch.dylib              0x3fa8 _dispatch_client_callout
25 libdispatch.dylib              0x745c _dispatch_continuation_pop
26 libdispatch.dylib              0x1b620 _dispatch_source_latch_and_call
27 libdispatch.dylib              0x1a1e8 _dispatch_source_invoke
28 libdispatch.dylib              0xb42c _dispatch_lane_serial_drain
29 libdispatch.dylib              0xc124 _dispatch_lane_invoke
30 libdispatch.dylib              0x1738c _dispatch_root_queue_drain_deferred_wlh
31 libdispatch.dylib              0x16bd8 _dispatch_workloop_worker_thread
32 libsystem_pthread.dylib        0x3680 _pthread_wqthread
33 libsystem_pthread.dylib        0x1474 start_wqthread

Log 2:

Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0x2d5fc __exceptionPreprocess
1  libobjc.A.dylib                0x31244 objc_exception_throw
2  CoreFoundation                 0x15548 -[NSMutableDictionary __addObject:forKey:]
3  CoreFoundation                 0x168f0 __72-[CFPrefsSource mergeIntoDictionary:sourceDictionary:cloudKeyEvaluator:]_block_invoke
4  CoreFoundation                 0x23ecc -[__NSFrozenDictionaryM __apply:context:]
5  CoreFoundation                 0x4f82c -[CFPrefsSource mergeIntoDictionary:sourceDictionary:cloudKeyEvaluator:]
6  CoreFoundation                 0x783b8 -[CFPrefsSearchListSource alreadylocked_getDictionary:]
7  CoreFoundation                 0x77dfc -[CFPrefsSearchListSource alreadylocked_copyValueForKey:]
8  CoreFoundation                 0x77d30 -[CFPrefsSource copyValueForKey:]
9  CoreFoundation                 0x77ce4 __76-[_CFXPreferences copyAppValueForKey:identifier:container:configurationURL:]_block_invoke
10 CoreFoundation                 0x77690 __108-[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:]_block_invoke
11 CoreFoundation                 0x5cf24 normalizeQuintuplet
12 CoreFoundation                 0x5cd60 -[_CFXPreferences withSearchListForIdentifier:container:cloudConfigurationURL:perform:]
13 CoreFoundation                 0x5cc60 -[_CFXPreferences copyAppValueForKey:identifier:container:configurationURL:]
14 CoreFoundation                 0x5c8bc _CFPreferencesCopyAppValueWithContainerAndConfiguration
15 Foundation                     0xf0dcc -[NSUserDefaults(NSUserDefaults) objectForKey:]
16 OneSignalCore                  0xc7c8 -[OneSignalUserDefaults keyExists:] + 61 (OneSignalUserDefaults.m:61)
17 OneSignalCore                  0xcbd8 -[OneSignalUserDefaults getSavedDoubleForKey:defaultValue:] + 107 (OneSignalUserDefaults.m:107)
18 OneSignalFramework             0x8964 +[OneSignal shouldStartNewSession] + 350 (OneSignal.m:350)
19 OneSignalFramework             0xc968 +[OneSignalTracker applicationBecameActive] + 83 (OneSignalTracker.m:83)
20 OneSignalFramework             0xb894 -[OneSignalLifecycleObserver didBecomeActive] + 84 (OneSignalLifecycleObserver.m:84)
......
55 UIKitCore                      0x3ee674 -[UIApplication _run]
56 UIKitCore                      0x14e88 UIApplicationMain
57 UnityFramework                 0x399aef0 -[UnityFramework runUIApplicationMainWithArgc:argv:] + 96 (main.mm:96)
58 ClientsApp                     0x412c main + 28 (main.mm:28)

I don’t suppose you have an Apple crash report for this? If so, please post it here. See Posting a Crash Report for advice on how to do that.

If not… well… things get trickier, but lemme know if that’s the case and I’ll see what I can do with what you have.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hi, thank you for the prompt response.

We reached out to ask for an Apple crash report. A long time has passed and we have not received one from the reporter. Is the provided information sufficient for now?

Calling [NSUserDefaults objectForKey:] crashes as it attempts to insert nil object into dictionary
 
 
Q