Resolved EXC_BAD_ACCESS with NSKeyedUnarchiver

Discussion in 'iOS Programming' started by silverj87, Mar 24, 2011.

  1. silverj87, Mar 24, 2011
    Last edited: Mar 25, 2011

    silverj87 macrumors newbie

    Joined:
    Mar 24, 2011
    #1
    Background: I'm getting EXC_BAD_ACCESS error with the NSKeyedUnarchiver. The archiving seems to be done correctly and I am able to follow the code through the debugger and see the object get created and unarchived, but the minute it attempts to return the object I get the bad access error. I've been reading up on all of the memory management stuff and I can't seem to find where i've gone wrong. Any help is appreciated:

    Here is the unarchive method:
    Code:
    - (void) unarchiveManager {
    	[self setWManager:[[NSKeyedUnarchiver unarchiveObjectWithFile:[self archivePath]] retain]]; // Here is where the error originates.
    }
    
    Here is the setWManager method which i believe conforms to the standards:
    Code:
    - (void) setWManager:(Manager *)newManager {
    	if (wManager != newManager) {
    		[wManager release];
    		wManager = [newManager retain];
    	}
    }
    
    One last thing, I tried something similar to below inside the unarchive method, but it didnt get past the keyedUnarchiver line either. I tried it with the 'retain' and without and arrived at the same outcome.
    Code:
    Manager *temp = [[NSKeyedUnarchiver unarchiveObjectWithFile:[self archivePath]] retain]; // Didnt get past this line either.
    [self setWManager:temp]; // Program never reaches this line.
    [temp release];
    
    Any more information needed I can do. Thanks!
     
  2. nickculbertson macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #2
    In your code you released something right before you referenced it.

    Code:
    Your code...
    - (void) setWManager:(Manager *)newManager {
    	if (wManager != newManager) {
    		[wManager release];
    		wManager = [newManager retain];
    	}
    }
    try this instead

    Code:
    - (void) setWManager:(Manager *)newManager {
    	if (wManager != newManager) {
    		wManager = [newManager retain];
                    [wManager release];
    	}
    }
    Nick
     
  3. silverj87 thread starter macrumors newbie

    Joined:
    Mar 24, 2011
    #3
    Referencing the design guides, i believe I am doing it correctly. However, it may be more helpful to know that I never even reach that method. If I set a break point on the 'if' statement at the beginning of the setter function I never reach it before getting the error.

    Also, when using the last method i show at the end of my first post, you can see that it never gets pass the assignment statement for the temp variable even though I can follow the code ALL the way until the object is returned and it runs fine up until then.

    Thanks for the help though, hope someone can fix this.
     
  4. nickculbertson macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #4
    What is "archivePath" pointing to? an NSString?
     
  5. silverj87 thread starter macrumors newbie

    Joined:
    Mar 24, 2011
    #5
    Yes, the archivepath is an NSString pointing to the previously defined path. I believe it is working since I can get the object to create, but just not return successfully... if that makes any sense.
     
  6. silverj87, Mar 24, 2011
    Last edited: Mar 24, 2011

    silverj87 thread starter macrumors newbie

    Joined:
    Mar 24, 2011
    #6
    Also, I just noticed something.... in the debugger, when I am watching the object get created i noticed that the objc_super variable in locals is 'out of scope' - Now when i reset the data and start from scratch, if I watch the initial object being created the objc_super is NOT out of scope. I am assuming this is a problem but am unsure of how to fix it. Does this help?

    ** Nevermind - It seems the objc_super is only out of scope just as it starts to 'return self;' Ive looked over some of my other code and that seems to be typically at the return point.
     
  7. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #7
    Problem with bad Access, is mostly a memory management problem.
    U could try to find it out by doing Command Shift A, (or Build and analyze), this should show u what u are doing wrong, (not allways, but it's just a guide of trying to debug faster).
    Good luck :)
     
  8. silverj87 thread starter macrumors newbie

    Joined:
    Mar 24, 2011
    #8
    Haha awesome tip. I knew it was a memory error but never knew about the build & analyze function... after about 30 minutes of cleanup up it runs smoothly now with no leaks and no crashes. Thanks for the help, I'm sure it was a couple of errors all piled up into one that got me. Thanks again.
     
  9. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #9
    No problem, It's one of the most awesome build in tools, because mostly it even shows u what's wrong exactly with arrows etc ;) so it's fairly easy to debug.
    Good to hear it's fixed. good luck :)
     

Share This Page