1

我在调用后立即在我的 ios 应用程序中遇到了 SIGSEGV 崩溃:

[[NSUserDefaults standardUserDefaults] synchronize];

这是在同步命令之后的崩溃堆栈跟踪的一部分:

CoreFoundation 0x31920232 CFCopyTypeIDDescription + 29
CoreFoundation 0x3191cd41 <redacted> + 204
CoreFoundation 0x31910815 <redacted> + 16
CoreFoundation 0x3188e1ac <redacted> + 140
CoreFoundation 0x3189d1ff CFDictionaryApplyFunction + 158
CoreFoundation 0x3191c483 <redacted> + 282
CoreFoundation 0x318b237f CFPropertyListWriteToStream + 138
CoreFoundation 0x318b115d <redacted> + 296
CoreFoundation 0x318b1031 <redacted> + 132
CoreFoundation 0x318b03c1 <redacted> + 492
CoreFoundation 0x318b01d1 <redacted> + 20
CoreFoundation 0x318bb3ff <redacted> + 110
CoreFoundation 0x318bb8fb <redacted> + 74
CoreFoundation 0x3193cb3b <redacted> + 30
CoreFoundation 0x3193d509 <redacted> + 32
libdispatch.dylib 0x39bd54b7 <redacted> + 22
libdispatch.dylib 0x39bd99f7 <redacted> + 30
CoreFoundation 0x318c7045 CFPreferencesAppSynchronize + 312

这是一次罕见的崩溃,所以我无法重现它,我的问题是:

什么会导致这样的崩溃?

4

2 回答 2

1

What I do to try and avoid this kind of situation, and to have better control of the defaults, is to funnel everything through my appDelegate (a singleton class works too). The idea is that when you want to save something, you send it to the designated object, in my case:

[appDelegate defaultsSetObject:(id)obj forKey:(NSSString *)key];

In that method I copy the obj and key (in case they are mutable objects). If I get into trouble like you are now, I can invoke synchronize on every call (to force an immediate failure), or at least log everything.

If your problem is hard to reproduce, spending the time to re-organize your defaults code might be worth it. On the other hand, you could create a macro, replace all your defaults code with the macro, and by redefining the macro get similar behavior to what I did. [The macro would be something like:

SAVE_OBJECT_FOR_KEY(obj, key);

In the simplest command case, just save the object. When debugging, do the copy as I suggest above, save, then synchronize.

于 2013-11-14T14:44:01.767 回答
0

很可能之前从用户默认值返回的某些对象已被过度释放。当包含对象的 NSUserDefaults 中的字典被序列化为 plist 时,当它尝试序列化现在已死的对象时会发生崩溃

于 2013-11-14T16:49:57.160 回答