I use the following code to create an NSDate from a specific format of an NSString.
+ (NSDateFormatter *)serverFormatter
{
static NSDateFormatter *formatter = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HHmmssZ"];
});
return formatter;
}
+ (NSDate *)dateFromServerDateString:(NSString *)dateString
{
if (!dateString) {
return nil;
}
NSDateFormatter *formatter = [NSDate serverFormatter];
NSString *cleaned = [dateString stringByReplacingOccurrencesOfString:@":"
withString:@""];
return [formatter dateFromString:cleaned];
}
Every once in a blue moon I'll receive an EXC_BAD_ACCESS on return [formatter dateFromString:cleaned];
Viewing the stack trace shows that cleaned, dateString, and formatter all have have their correct values (not nil and 'po' prints a correct value in the console).
After doing some reading, I did discover that my code needs to take into consideration thread safety on NSDateFormatter. (And I have appropriately adjusted my code which is not shown here.) However according to the stack traces, only one thread is accessing my NSDateFormatter when the crash occurred.
The stack trace with the exception looks as shown below.
#0 0x02d09ad1 in typeinfo name for icu::UObject ()
#1 0x02b8f5ed in icu::TimeZone::getOffset(double, signed char, int&, int&, UErrorCode&) const ()
#2 0x02b95652 in icu::Calendar::computeTime(UErrorCode&) ()
#3 0x02b95552 in icu::Calendar::updateTime(UErrorCode&) ()
#4 0x02b96036 in icu::Calendar::getTimeInMillis(UErrorCode&) const ()
#5 0x02c4244f in icu::DateFormat::parse(icu::UnicodeString const&, icu::ParsePosition&) const ()
#6 0x02cebd05 in udat_parse ()
#7 0x029b7161 in CFDateFormatterGetAbsoluteTimeFromString ()
#8 0x029b6f8e in CFDateFormatterCreateDateFromString ()
#9 0x0214e7e9 in getObjectValue ()
#10 0x0214e701 in -[NSDateFormatter getObjectValue:forString:errorDescription:] ()
#11 0x0214ea3f in -[NSDateFormatter dateFromString:] ()
#12 0x0001a7a4 in +[NSDate(RDUtilities) dateFromServerDateString:]
...
Has anyone encountered anything like this before and has any tips for debugging?