According to Instruments, my object is sent a release message on this line:
[currentDefaults setObject:self.myArray forKey:@"storedArray"]
This is causing over-release and crash. NSZombies when Profiled found this line and the retain count issue.
But why? The synthesized property is this:
@property (nonatomic, copy) NSArray*myArray;
Why is the line at the top causing the retain count to change?
Here is the backtrace after crash (the line 103 mentioned is the line above):
(lldb) bt
* thread #20: tid = 0x3103, 0x01e31276 CoreFoundation`CFRetain + 22, stop reason = EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)
frame #0: 0x01e31276 CoreFoundation`CFRetain + 22
frame #1: 0x01eeca00 CoreFoundation`__CFDictionaryStandardRetainValue + 80
frame #2: 0x01e74e62 CoreFoundation`__CFBasicHashReplaceValue + 34
frame #3: 0x01e41964 CoreFoundation`CFBasicHashSetValue + 2628
frame #4: 0x01e40ee3 CoreFoundation`CFDictionarySetValue + 227
frame #5: 0x01e773b5 CoreFoundation`-[CFXPreferencesSource setValue:forKey:] + 85
frame #6: 0x01e8a81e CoreFoundation`-[CFXPreferencesPropertyListSource setValue:forKey:] + 110
frame #7: 0x01e8a72f CoreFoundation`_CFXPreferencesSetValue + 159
frame #8: 0x01e8a5b3 CoreFoundation`CFPreferencesSetAppValue + 51
frame #9: 0x01372ceb Foundation`-[NSUserDefaults(NSUserDefaults) setObject:forKey:] + 100
frame #10: 0x00003922 Ivory Bull Charts`__21-[SavedData saveData]_block_invoke_0 + 178 at SavedData.m:103
frame #11: 0x017a3330 libdispatch.dylib`_dispatch_call_block_and_release + 15
frame #12: 0x017a4439 libdispatch.dylib`_dispatch_worker_thread2 + 302
frame #13: 0x996a3b24 libsystem_c.dylib`_pthread_wqthread + 346
(lldb)
Make a copy of your arrays or dictionaries and store those in user defaults, rather than the instance variables themselves and see if it prevents retain issues when you re-store the same variable contents to the user defaults.