![]() |
|
Unarchiving Objective-C objects - Printable Version +- iDevGames Forums (http://www.idevgames.com/forums) +-- Forum: Development Zone (/forum-3.html) +--- Forum: Programming Languages & Scripting (/forum-8.html) +--- Thread: Unarchiving Objective-C objects (/thread-9329.html) |
Unarchiving Objective-C objects - sealfin - Sep 12, 2011 01:26 PM Long story short - I'm trying to learn the few bits of Objective-C/Cocoa I've (for years) neglected/never needed to use; amongst the bits: archiving and unarchiving objects. Unfortunately, I've hit a bug I can't seem to fix: whilst I can archive and unarchive the objects, and send messages to the unarchived objects, any messages sent to the unarchived objects after the NSKeyedUnarchiver object used to unarchive the objects is sent the release message causes a crash due to EXC_BAD_ACCESS; I thought I understood the Objective-C memory management rules - it seems I was mistaken... I'd appreciate it if anybody could take a look at my code and tell me where I'm going wrong... (Edit: I've uploaded the source code here.) The code for unarchiving the objects, with the lines that cause the crash marked: Code: TestObject *one, *two;The code for archiving the objects: Code: TestObject *one = [[TestObject alloc] initWithString:@"one"];The code to the class: Code: static int g_int = 1;RE: Unarchiving Objective-C objects - sealfin - Sep 12, 2011 01:44 PM Okay, I'm an idiot: I just noticed I forgot to Code: [m_string retain];Code: m_string = [p_decoder decodeObjectForKey:@"theString"];![]() A pity I never noticed that omission during the hour I spent staring at the code before I started this thread
RE: Unarchiving Objective-C objects - monteboyd - Sep 12, 2011 05:39 PM Sometimes the most productive hour you can spend coding is to spend an hour not coding, away from the screen.
RE: Unarchiving Objective-C objects - ipeku - Sep 12, 2011 06:00 PM (Sep 12, 2011 01:44 PM)sealfin Wrote: Okay, I'm an idiot: I just noticed I forgot to This is easy to forget. The unarchiver returns an object that has a retain count of zero. Sometimes you get away with it if you assign to a property with the (retain) attribute (i.e., @property (retain) ... *myObject; and you call self.myObject=[p_decoder decode....]; when Objective-C automatically retains it for you. At times you forget that "self." and wonder why it's crashing. RE: Unarchiving Objective-C objects - Skorche - Sep 13, 2011 06:33 AM (Sep 12, 2011 05:39 PM)monteboyd Wrote: Sometimes the most productive hour you can spend coding is to spend an hour not coding, away from the screen. Amen to that. It's happened to me a number of times where I will spend hours looking for a bug only to find out and finally give up. Then shortly after while not at my computer (and often far away from it) the solution will just pop right into my head. RE: Unarchiving Objective-C objects - GoodDoug - Sep 15, 2011 02:24 PM The static analyzer in XCode 4 should have found that for you. Hitting Command-Shift-B on my project always makes me go "DOH!" for some error like that. |