In the app I’m writing,
NSDate *test = [NSDate distantPast];
[test retain]; // crashes with EXC_BAD_ACCESS
crashes almost every time on retain, while
NSDate *test = [NSDate date];
[test retain];
does never.
I am puzzled why. The piece of code is called on the main thread through an UI action and I’m running it in the IOS simulator in the debugger. Sometimes it works without crashing. Might it be a concurrency issue? Is there an obvious problem that I am missing or should I try trimming down my application until it works?
UPDATE:
NSDate *test = [[NSDate distantPast] copy];
crashes as well, while
NSDate *test = [[NSDate date] copy];
doesn’t. Cleaning the build and restarting the IOS simulator didn’t help either.
UPDATE 2:
Calling the snippet in application:didFinishLaunchingWithOptions does not crash. My app must be doing something that messes up the system. I’ll post an answer when I find out what.
UPDATE 3 (SOLVED):
The offending line was somewhere completely different in my application:
WRONG:
- (id)init
{
lastUpdate = [NSDate distantPast];
}
The instance of distantPast gets autoreleased after the execution leaves the init code block. I should have retained it to counter that autorelease.
CORRECT:
- (id)init
{
lastUpdate = [[NSDate distantPast] retain];
}
Profile your app with the “Zombies” instrument. When it crashes, you should be able to see the full retain/release history of the rogue object, and from that figure out what went wrong.