1
votes

I have these two lines inside a block that is called when the app is about to quit

NSTimeZone* systemTimeZone = [NSTimeZone systemTimeZone];
NSTimeInterval delta = [systemTimeZone daylightSavingTimeOffset];

I have these lines there for weeks. They were working perfectly. Now, the app crashes on the second line showing this error:

-[NSCFString daylightSavingTimeOffset]: unrecognized selector sent to instance 0x1534b0

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString daylightSavingTimeOffset]: unrecognized selector sent to instance 0x1534b0'**

what? any clues? thanks.

1
Are those lines actually running one after another like you have in your example?Joe
I was asking because it is strange to for a call to a method that returns one type is already returned as another type (NSString instead of NSTimeZone). I did some digging around and found a possible cause which I posted below. Hope that helps.Joe

1 Answers

5
votes

If those lines are being called in order and you are getting that message it looks you are over releasing the systemTimeZone somewhere. It just so happens that a valid NSString* is occupying the memory where the cached systemTimeZone was stored.

According to the documentation:

Special Considerations
If you get the system time zone, it is cached by the application and does not change if the user subsequently changes the system time zone. The next time you invoke systemTimeZone, you get back the same time zone you originally got. You have to invoke resetSystemTimeZone to clear the cached object.

So consider this

NSTimeZone *systemTimeZone = [NSTimeZone systemTimeZone];
NSLog(@"%lf", [systemTimeZone daylightSavingTimeOffset]); //This works
[systemTimeZone release]; //Testing release do not actually do this

systemTimeZone = [NSTimeZone systemTimeZone];
NSLog(@"%lf", [systemTimeZone daylightSavingTimeOffset]); //This works
[systemTimeZone release]; //Testing release do not actually do this

systemTimeZone = [NSTimeZone systemTimeZone];
NSLog(@"%lf", [systemTimeZone daylightSavingTimeOffset]); //This crashes EXC_BAD_ACCESS
//The cached systemTimeZone at this point has been over released.

Where I get my EXC_BAD_ACCESS it would also be possible for an NSString* to reside there as I mentioned earlier and that would result in exception being raised. So just make sure you are not improperly releasing it anywhere.