Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

silverj87

macrumors newbie
Original poster
Mar 24, 2011
6
0
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!
 
Last edited:

nickculbertson

macrumors regular
Nov 19, 2010
226
0
Nashville, TN
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
 

silverj87

macrumors newbie
Original poster
Mar 24, 2011
6
0
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.
 

silverj87

macrumors newbie
Original poster
Mar 24, 2011
6
0
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.
 

silverj87

macrumors newbie
Original poster
Mar 24, 2011
6
0
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.
 
Last edited:

jnoxx

macrumors 65816
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
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 :)
 

silverj87

macrumors newbie
Original poster
Mar 24, 2011
6
0
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.
 

jnoxx

macrumors 65816
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
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.

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 :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.