I am trying to read the mdm config from UserDefaults but it keeps crashing.
The code is pretty straight forward:
let defaults = UserDefaults.standard;
let serverConfig = defaults.dictionary(forKey: Settings.SETTING_MANAGED_KCONFIGURATIONURL);
Whereas SETTING_MANAGED_KCONFIGURATIONURL is set to: "com.apple.configuration.managed"
Its crashing on the second line.
I am also using the UserDefaults.didChangeNotification Observer.
Here is my crashlog:
Exception Subtype: KERN_PROTECTION_FAILURE at 0x000000016f097e78
VM Region Info: 0x16f097e78 is in 0x16f094000-0x16f098000; bytes after start: 15992 bytes before end: 391
REGION TYPE START - END [ VSIZE] PRT/MAX SHRMOD REGION DETAIL
MALLOC_TINY 0000000115800000-0000000115900000 [ 1024K] rw-/rwx SM=PRV
GAP OF 0x59794000 BYTES
---> STACK GUARD 000000016f094000-000000016f098000 [ 16K] ---/rwx SM=NUL ... for thread 0
Stack 000000016f098000-000000016f194000 [ 1008K] rw-/rwx SM=PRV thread 0
Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL, Code 0xb
Terminating Process: exc handler [560]
Triggered by Thread: 0
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 CoreFoundation 0x00000001926ffee8 __CFStringAppendBytes + 188
1 CoreFoundation 0x00000001926f2528 __CFStringAppendFormatCore + 10180
2 CoreFoundation 0x00000001926f2528 __CFStringAppendFormatCore + 10180
3 CoreFoundation 0x00000001926f35bc _CFStringCreateWithFormatAndArgumentsAux2 + 136
4 CoreFoundation 0x00000001926f3664 CFStringCreateWithFormat + 44
5 CoreFoundation 0x00000001926c047c -[CFPrefsSearchListSource copyOSLogDescription] + 116
6 CoreFoundation 0x0000000192630eb8 -[CFPrefsSearchListSource alreadylocked_copyValueForKey:] + 260
7 CoreFoundation 0x0000000192630d90 -[CFPrefsSource copyValueForKey:] + 60
8 CoreFoundation 0x000000019277cae8 __76-[_CFXPreferences copyAppValueForKey:identifier:container:configurationURL:]_block_invoke + 40
9 CoreFoundation 0x00000001926c0c54 __108-[_CFXPreferences+ 617556 (SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:]_block_invoke + 272
10 CoreFoundation 0x00000001926c0698 normalizeQuintuplet + 340
11 CoreFoundation 0x000000019262ed64 -[_CFXPreferences+ 19812 (SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:] + 108
12 CoreFoundation 0x000000019262f5f0 -[_CFXPreferences copyAppValueForKey:identifier:container:configurationURL:] + 148
13 CoreFoundation 0x000000019277ef30 _CFPreferencesCopyAppValueWithContainerAndConfiguration + 124
14 Foundation 0x000000019309c8fc -[NSUserDefaults+ 26876 (NSUserDefaults) objectForKey:] + 52
15 MYAPP 0x0000000100cd3bfc MISettingsManager.checkAndReadMDMSettings() + 424956 (MISettingsManager.swift:303)
16 MYAPP 0x0000000100cda2b0 specialized closure #1 in static MISettingsManager.instance.getter + 451248 (<compiler-generated>:0)
17 MYAPP 0x0000000100ccdb5c thunk for @escaping @callee_guaranteed (@in_guaranteed Notification) -> () + 400220 (<compiler-generated>:0)
18 Foundation 0x00000001930c5ef0 -[__NSObserver _doit:] + 316
19 CoreFoundation 0x00000001926b421c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
20 CoreFoundation 0x00000001926b41e8 ___CFXRegistrationPost_block_invoke + 64
21 CoreFoundation 0x00000001926b36dc _CFXRegistrationPost + 392
22 CoreFoundation 0x00000001926b3388 ___CFXNotificationPost_block_invoke + 96
23 CoreFoundation 0x000000019262cc54 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1496
24 CoreFoundation 0x00000001926b2e38 _CFXNotificationPost + 696
25 Foundation 0x000000019309c1a4 -[NSNotificationCenter postNotificationName:object:userInfo:] + 68
26 MYAPP 0x0000000100cd2b64 MISettingsManager.setValueForSetting(_:_:) + 420708 (<compiler-generated>:0)
27 MYAPP 0x0000000100cd4900 MISettingsManager.setSettingsValue(key:value:) + 428288 (MISettingsManager.swift:364)
28 MYAPP 0x0000000100cd5990 MISettingsManager.validateMDMSettings(url:system:user:) + 432528 (MISettingsManager.swift:411)
29 MYAPP 0x0000000100cd3fc4 MISettingsManager.checkAndReadMDMSettings() + 425924 (<compiler-generated>:0)
30 MYAPP 0x0000000100cda2b0 specialized closure #1 in static MISettingsManager.instance.getter + 451248 (<compiler-generated>:0)
31 MYAPP 0x0000000100ccdb5c thunk for @escaping @callee_guaranteed (@in_guaranteed Notification) -> () + 400220 (<compiler-generated>:0)
32 Foundation 0x00000001930c5ef0 -[__NSObserver _doit:] + 316
33 CoreFoundation 0x00000001926b421c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
34 CoreFoundation 0x00000001926b41e8 ___CFXRegistrationPost_block_invoke + 64
35 CoreFoundation 0x00000001926b36dc _CFXRegistrationPost + 392
36 CoreFoundation 0x00000001926b3388 ___CFXNotificationPost_block_invoke + 96
37 CoreFoundation 0x000000019262cc54 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1496
38 CoreFoundation 0x00000001926b2e38 _CFXNotificationPost + 696
39 Foundation 0x000000019309c1a4 -[NSNotificationCenter postNotificationName:object:userInfo:] + 68
40 MYAPP 0x0000000100cd2b64 MISettingsManager.setValueForSetting(_:_:) + 420708 (<compiler-generated>:0)
41 MYAPP 0x0000000100cd4900 MISettingsManager.setSettingsValue(key:value:) + 428288 (MISettingsManager.swift:364)
42 MYAPP 0x0000000100cd5990 MISettingsManager.validateMDMSettings(url:system:user:) + 432528 (MISettingsManager.swift:411)
43 MYAPP 0x0000000100cd3fc4 MISettingsManager.checkAndReadMDMSettings() + 425924 (<compiler-generated>:0)
44 MYAPP 0x0000000100cda2b0 specialized closure #1 in static MISettingsManager.instance.getter + 451248 (<compiler-generated>:0)
45 MYAPP 0x0000000100ccdb5c thunk for @escaping @callee_guaranteed (@in_guaranteed Notification) -> () + 400220 (<compiler-generated>:0)
46 Foundation 0x00000001930c5ef0 -[__NSObserver _doit:] + 316
47 CoreFoundation 0x00000001926b421c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
48 CoreFoundation 0x00000001926b41e8 ___CFXRegistrationPost_block_invoke + 64
49 CoreFoundation 0x00000001926b36dc _CFXRegistrationPost + 392
50 CoreFoundation 0x00000001926b3388 ___CFXNotificationPost_block_invoke + 96
51 CoreFoundation 0x000000019262cc54 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1496
52 CoreFoundation 0x00000001926b2e38 _CFXNotificationPost + 696
53 Foundation 0x000000019309c1a4 -[NSNotificationCenter postNotificationName:object:userInfo:] + 68
54 MYAPP 0x0000000100cd2b64 MISettingsManager.setValueForSetting(_:_:) + 420708 (<compiler-generated>:0)
55 MYAPP 0x0000000100cd4900 MISettingsManager.setSettingsValue(key:value:) + 428288 (MISettingsManager.swift:364)
56 MYAPP 0x0000000100cd5990 MISettingsManager.validateMDMSettings(url:system:user:) + 432528 (MISettingsManager.swift:411)
57 MYAPP 0x0000000100cd3fc4 MISettingsManager.checkAndReadMDMSettings() + 425924 (<compiler-generated>:0)
58 MYAPP 0x0000000100cda2b0 specialized closure #1 in static MISettingsManager.instance.getter + 451248 (<compiler-generated>:0)
59 MYAPP 0x0000000100ccdb5c thunk for @escaping @callee_guaranteed (@in_guaranteed Notification) -> () + 400220 (<compiler-generated>:0)
60 Foundation 0x00000001930c5ef0 -[__NSObserver _doit:] + 316
61 CoreFoundation 0x00000001926b421c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
62 CoreFoundation 0x00000001926b41e8 ___CFXRegistrationPost_block_invoke + 64
63 CoreFoundation 0x00000001926b36dc _CFXRegistrationPost + 392
64 CoreFoundation 0x00000001926b3388 ___CFXNotificationPost_block_invoke + 96
65 CoreFoundation 0x000000019262cc54 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1496
66 CoreFoundation 0x00000001926b2e38 _CFXNotificationPost + 696
67 Foundation 0x000000019309c1a4 -[NSNotificationCenter postNotificationName:object:userInfo:] + 68
68 MYAPP 0x0000000100cd2b64 MISettingsManager.setValueForSetting(_:_:) + 420708 (<compiler-generated>:0)
69 MYAPP 0x0000000100cd4900 MISettingsManager.setSettingsValue(key:value:) + 428288 (MISettingsManager.swift:364)
70 MYAPP 0x0000000100cd5990 MISettingsManager.validateMDMSettings(url:system:user:) + 432528 (MISettingsManager.swift:411)
71 MYAPP 0x0000000100cd3fc4 MISettingsManager.checkAndReadMDMSettings() + 425924 (<compiler-generated>:0)
72 MYAPP 0x0000000100cda2b0 specialized closure #1 in static MISettingsManager.instance.getter + 451248 (<compiler-generated>:0)
73 MYAPP 0x0000000100ccdb5c thunk for @escaping @callee_guaranteed (@in_guaranteed Notification) -> () + 400220 (<compiler-generated>:0)
74 Foundation 0x00000001930c5ef0 -[__NSObserver _doit:] + 316
75 CoreFoundation 0x00000001926b421c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
76 CoreFoundation 0x00000001926b41e8 ___CFXRegistrationPost_block_invoke + 64
77 CoreFoundation 0x00000001926b36dc _CFXRegistrationPost + 392
78 CoreFoundation 0x00000001926b3388 ___CFXNotificationPost_block_invoke + 96
79 CoreFoundation 0x000000019262cc54 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1496
80 CoreFoundation 0x00000001926b2e38 _CFXNotificationPost + 696
81 Foundation 0x000000019309c1a4 -[NSNotificationCenter postNotificationName:object:userInfo:] + 68
82 MYAPP 0x0000000100cd2b64 MISettingsManager.setValueForSetting(_:_:) + 420708 (<compiler-generated>:0)
83 MYAPP 0x0000000100cd4900 MISettingsManager.setSettingsValue(key:value:) + 428288 (MISettingsManager.swift:364)
84 MYAPP 0x0000000100cd5990 MISettingsManager.validateMDMSettings(url:system:user:) + 432528 (MISettingsManager.swift:411)
85 MYAPP 0x0000000100cd3fc4 MISettingsManager.checkAndReadMDMSettings() + 425924 (<compiler-generated>:0)
86 MYAPP 0x0000000100cda2b0 specialized closure #1 in static MISettingsManager.instance.getter + 451248 (<compiler-generated>:0)
87 MYAPP 0x0000000100ccdb5c thunk for @escaping @callee_guaranteed (@in_guaranteed Notification) -> () + 400220 (<compiler-generated>:0)
88 Foundation 0x00000001930c5ef0 -[__NSObserver _doit:] + 316
89 CoreFoundation 0x00000001926b421c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
90 CoreFoundation 0x00000001926b41e8 ___CFXRegistrationPost_block_invoke + 64
91 CoreFoundation 0x00000001926b36dc _CFXRegistrationPost + 392
92 CoreFoundation 0x00000001926b3388 ___CFXNotificationPost_block_invoke + 96
93 CoreFoundation 0x000000019262cc54 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1496
94 CoreFoundation 0x00000001926b2e38 _CFXNotificationPost + 696
95 Foundation 0x000000019309c1a4 -[NSNotificationCenter postNotificationName:object:userInfo:] + 68
[...]
Any help would be nice. Thanks!
[UPDATE]
I changed my code slightly to see what is really returned. It's still crashing but on at a different spot. Which doesn't makes sense since I haven't changed the logic. The first 5 lines are just for testing purpose and can be ignored. But I also added the PushNotification as mentioned here. Unfortunately there wasn't an explanation on why to add this.
let defaults = UserDefaults.standard;
let server = defaults.object(forKey: Settings.SETTING_MANAGED_KCONFIGURATIONURL);
print("[MISettingsManager] checkAndReadMDMSettings - server: \(server ?? "NULL")");
if let server = server as! [String:Any]? {
print("[MISettingsManager] readManagedDefaultValues - \(server.description)");
}
let serverConfig = defaults.dictionary(forKey: Settings.SETTING_MANAGED_KCONFIGURATIONURL);
if(nil == serverConfig) {
return;
}
print("[MISettingsManager] readManagedDefaultValues - \(serverConfig!.description)");
let url: String? = serverConfig![Settings.SETTING_SERVER_SERVICEURL] as? String;
if (!(url ?? "").isEmpty) {
self.setSettingsValue(key: Settings.SETTING_SERVER_SERVICEURL, value: url);
}
setSettingsValue is written like this. Its also where its crashing. Null value check is done before this.
self.setValueForSetting(key!, value!);
This is the new crashlog:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_PROTECTION_FAILURE at 0x000000016eeb3b10
VM Region Info: 0x16eeb3b10 is in 0x16eeb0000-0x16eeb4000; bytes after start: 15120 bytes before end: 1263
REGION TYPE START - END [ VSIZE] PRT/MAX SHRMOD REGION DETAIL
mapped file 0000000104bf0000-0000000104d48000 [ 1376K] r--/rw- SM=COW
GAP OF 0x6a168000 BYTES
---> STACK GUARD 000000016eeb0000-000000016eeb4000 [ 16K] ---/rwx SM=NUL ... for thread 0
Stack 000000016eeb4000-000000016efb0000 [ 1008K] rw-/rwx SM=PRV thread 0
Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL, Code 0xb
Terminating Process: exc handler [607]
Triggered by Thread: 0
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 CoreFoundation 0x00000001927711c4 CFBasicHashCreateCopy + 4
1 CoreFoundation 0x00000001926a1664 CFDictionaryCreateMutableCopy + 180
2 CoreFoundation 0x00000001926308a4 -[_CFXNotificationNameWildcardObjectRegistration find:matching:] + 308
3 CoreFoundation 0x000000019262cbdc -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1376
4 CoreFoundation 0x00000001926b2e38 _CFXNotificationPost + 696
5 Foundation 0x000000019309c1a4 -[NSNotificationCenter postNotificationName:object:userInfo:] + 68
6 MYAPP 0x0000000100eb693c MISettingsManager.setValueForSetting(_:_:) + 420156 (<compiler-generated>:0)
7 MYAPP 0x0000000100eb8920 MISettingsManager.setSettingsValue(key:value:) + 428320 (MISettingsManager.swift:375)
8 MYAPP 0x0000000100eb99b0 MISettingsManager.validateMDMSettings(url:system:user:) + 432560 (MISettingsManager.swift:422)
9 MYAPP 0x0000000100eb7fe4 MISettingsManager.checkAndReadMDMSettings() + 425956 (<compiler-generated>:0)
10 MYAPP 0x0000000100ebe2d0 specialized closure #1 in static MISettingsManager.instance.getter + 451280 (<compiler-generated>:0)
11 MYAPP 0x0000000100eb1b24 thunk for @escaping @callee_guaranteed (@in_guaranteed Notification) -> () + 400164 (<compiler-generated>:0)
12 Foundation 0x00000001930c5ef0 -[__NSObserver _doit:] + 316
13 CoreFoundation 0x00000001926b421c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
14 CoreFoundation 0x00000001926b41e8 ___CFXRegistrationPost_block_invoke + 64
15 CoreFoundation 0x00000001926b36dc _CFXRegistrationPost + 392
16 CoreFoundation 0x00000001926b3388 ___CFXNotificationPost_block_invoke + 96
17 CoreFoundation 0x000000019262cc54 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1496
18 CoreFoundation 0x00000001926b2e38 _CFXNotificationPost + 696
19 Foundation 0x000000019309c1a4 -[NSNotificationCenter postNotificationName:object:userInfo:] + 68
20 MYAPP 0x0000000100eb693c MISettingsManager.setValueForSetting(_:_:) + 420156 (<compiler-generated>:0)
21 MYAPP 0x0000000100eb8920 MISettingsManager.setSettingsValue(key:value:) + 428320 (MISettingsManager.swift:375)
22 MYAPP 0x0000000100eb99b0 MISettingsManager.validateMDMSettings(url:system:user:) + 432560 (MISettingsManager.swift:422)
23 MYAPP 0x0000000100eb7fe4 MISettingsManager.checkAndReadMDMSettings() + 425956 (<compiler-generated>:0)
24 MYAPP 0x0000000100ebe2d0 specialized closure #1 in static MISettingsManager.instance.getter + 451280 (<compiler-generated>:0)
25 MYAPP 0x0000000100eb1b24 thunk for @escaping @callee_guaranteed (@in_guaranteed Notification) -> () + 400164 (<compiler-generated>:0)
26 Foundation 0x00000001930c5ef0 -[__NSObserver _doit:] + 316
```