EXC_BAD_ACCESS (SIGSEGV) crash observed in NSDateFormatter APIs

Hi Team,

I am using NSDateFormatter to print date and time in my logger class.

// Get current date and time
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *dateTimeString = [dateFormatter stringFromDate:[NSDate date]];

I am using the NSDateFormatter as shown in the above snippet, but I am seeing random crashes with the below stack trace. This crash is random and can't be reproduced consistently.

0   libobjc.A.dylib               	    0x7ff8051a021d objc_msgSend + 29
1   CoreFoundation                	    0x7ff8056609ef __CFDateFormatterSetSymbolsArray + 59
2   CoreFoundation                	    0x7ff80564db87 __ApplyUDateFormatSymbol + 324
3   CoreFoundation                	    0x7ff80564c854 __ResetUDateFormat + 3064
4   CoreFoundation                	    0x7ff80564bc30 __CreateCFDateFormatter + 320
5   Foundation                    	    0x7ff8064c2ae3 -[NSDateFormatter _regenerateFormatter] + 323
6   Foundation                    	    0x7ff8064c2858 -[NSDateFormatter stringForObjectValue:] + 297
Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000018
Exception Codes:       0x0000000000000001, 0x0000000000000018

Termination Reason:    Namespace SIGNAL, Code 11 Segmentation fault: 11
Terminating Process:   exc handler [5372]

Can someone please suggest if there is something wrong in the way NSDateFormatter being used?

I checked the documentation and found nothing that can be causing this issue.

Thanks and Regards

I think you have to set your date formatter's locale before setting the format:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:410220000];
 
// US English Locale (en_US)
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocalizedDateFormatFromTemplate:@"MMMMd"]; // set template after setting locale
NSLog(@"%@", [dateFormatter stringFromDate:date]); // December 31
 
// British English Locale (en_GB)
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
[dateFormatter setLocalizedDateFormatFromTemplate:@"MMMMd"]; // set template after setting locale
NSLog(@"%@", [dateFormatter stringFromDate:date]); // 31 December
I think you have to set your date formatter's locale before setting the format

True but:

  • The code snippet you posted doesn’t need this because you’re calling -setLocalizedDateFormatFromTemplate:.

  • That might cause it to generate wrong results. It shouldn’t crash.

Can someone please suggest if there is something wrong in the way NSDateFormatter being used?

If you’re setting a fixed-format string like this, you must set the locale to something, typically en_US_POSIX, beforehand. See QA1480 NSDateFormatter and Internet Dates.

However, that shouldn’t cause it to crash like.

An obvious potential problem here is thread safety. However, in theory at least, NSDateFormatter should be thread safe.

Have you tried running your app with the standard memory debugging tools?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

EXC_BAD_ACCESS (SIGSEGV) crash observed in NSDateFormatter APIs
 
 
Q