Question

We are using dateFromComponents: to parse a date string returned from Server.

In rare cases this call crashes, but we couldn't reproduce the bug. Has anyone seen this kind of stack trace?

Thread 0:
0   libicucore.A.dylib                  0x3b13514e _uprv_asciitolower + 18
1   libicucore.A.dylib                  0x3b18757b _uloc_openKeywordList + 291
2   libicucore.A.dylib                  0x3b135d7f _uloc_getName + 15
3   libicucore.A.dylib                  0x3b135ba7 __ZN3icu6Locale4initEPKca + 175
4   libicucore.A.dylib                  0x3b14f359 __ZN3icu6Locale14createFromNameEPKc + 57
5   libicucore.A.dylib                  0x3b1b13db __ZN3icu8Calendar11setWeekDataERKNS_6LocaleEPKcR10UErrorCode + 131
6   libicucore.A.dylib                  0x3b1b164f __ZN3icu8CalendarC2ERKNS_8TimeZoneERKNS_6LocaleER10UErrorCode + 123
7   libicucore.A.dylib                  0x3b1d5857 __ZN3icu17GregorianCalendarC2ERKNS_8TimeZoneER10UErrorCode + 27
8   libicucore.A.dylib                  0x3b1d5d7b __ZN3icu17GregorianCalendar18setGregorianChangeEdR10UErrorCode + 163
9   CoreFoundation                      0x33728967 __CFCalendarSetupCal + 95
10  CoreFoundation                      0x336a5db1 __CFCalendarComposeAbsoluteTimeV + 41
11  CoreFoundation                      0x336b70d3 -[__NSCFCalendar dateFromComponents:] + 1139
12  <OurApp>                            0x0015d7c5 -[NSString(<ACategory>) parsedDateFromyyyyMMddFormat] + 493
13  <OurApp>                            0x000e28ab -[<AnNSOperation> finishedWithData:serverDate:] (<AnNSOperation>.m:154)
14  <OurApp>                            0x0009ba87 __49-[<AnotherNSOperation> connectionDidFinishLoading:]_block_invoke (<AnotherNSOperation>.m:283)
15  libdispatch.dylib                   0x3b9d0793 _dispatch_call_block_and_release + 11
16  libdispatch.dylib                   0x3b9d05db _dispatch_client_callout + 23
17  libdispatch.dylib                   0x3b9d3e45 __dispatch_main_queue_callback_4CF + 229
18  CoreFoundation                      0x336f01b1 __CFRunLoopRun + 1289
19  CoreFoundation                      0x3366323d _CFRunLoopRunSpecific + 357
20  CoreFoundation                      0x336630c9 _CFRunLoopRunInMode + 105
21  GraphicsServices                    0x3721e33b _GSEventRunModal + 75
22  UIKit                               0x3557f2b9 _UIApplicationMain + 1121
23  <OurApp>                            0x0006c151 main (main.m:32)

We also observe stack traces like this:

Thread 20 Crashed:
0   libsystem_kernel.dylib              0x3c4fb350 ___pthread_kill + 8
1   libsystem_c.dylib                   0x3c4ae36b _abort + 95
2   libc++abi.dylib                     0x3ba56ddf abort_message + 75
3   libc++abi.dylib                     0x3ba541cf ___cxa_pure_virtual + 19
4   libicucore.A.dylib                  0x3bc02637 __ZN3icu8CalendarC2ERKNS_8TimeZoneERKNS_6LocaleER10UErrorCode + 99
5   libicucore.A.dylib                  0x3bc26857 __ZN3icu17GregorianCalendarC2ERKNS_8TimeZoneER10UErrorCode + 27
6   libicucore.A.dylib                  0x3bc26d7b __ZN3icu17GregorianCalendar18setGregorianChangeEdR10UErrorCode + 163
7   CoreFoundation                      0x34189967 __CFCalendarSetupCal + 95
8   CoreFoundation                      0x34106db1 __CFCalendarComposeAbsoluteTimeV + 41
9   CoreFoundation                      0x341180d3 -[__NSCFCalendar dateFromComponents:] + 1139

Here are the methods:

- (NSDate *)parsedDateFromyyyyMMddFormat
{
    NSDateComponents* dateComp = [[NSDateComponents alloc] init];

    NSArray *stringParts = [self componentsSeparatedByString:@"-"];

    /* 
       check stringParts validity 
       ...
       get years, month & co from "stringParts"
       ...
       set year, month & co on "dateComp"
    */

    >>>> THIS LINE CRASHED 
    NSDate *date = [[[self class] currentCalendar] dateFromComponents: dateComp]; 
    <<<<

    return date;
}


+ (NSCalendar *)currentCalendar
{
    static NSCalendar *calendar;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        calendar = [NSCalendar currentCalendar];
        [calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
        [calendar setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"]];
    });

    return calendar;
}
Was it helpful?

Solution

If something happens randomly and rarely, its a red flag for a concurrency issue.

NSCalendar is not thread safe, make sure every use of the current calendar in your application is on the same thread.

Also, i dont know if Arc is smart enough to not retain your already singleton calendar twice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top