A random crash on NSDateFormater

My App keep getting NSDateFormatter crash randomly when I check iConnect on AppleStore, I did a lot search regarding of NSDataFormatter crash, mostly the reason is memory leak,


However, For me, it looks like Thread safe related,

here is stack



Thread 1 name:

Thread 1 Crashed:

0 libdispatch.dylib 0x0000000184d07b58 _dispatch_semaphore_dispose$VARIANT$mp + 76 (semaphore.c:69)

1 libdispatch.dylib 0x0000000184d06c7c _dispatch_dispose$VARIANT$mp + 80 (object.c:222)

2 Foundation 0x0000000185cdf7d0 -[NSDateFormatter dealloc] + 76 (NSDateFormatter.m:291)

3 App 0x0000000100e1a3d4 +[WICPage dateFormatter] (WICPage.m:80)

4 App 0x0000000100da6014 -[PagerSettings setServerTime:] (PagerSettings.m:2271)

5 App 0x0000000100db0f4c -[WICDownLinkPDU parseRawData:] (WICDownLinkPDU.m:368)

6 App 0x0000000100db06b8 -[WICDownLinkPDU initWithRawData:] (WICDownLinkPDU.m:254)

7 App 0x0000000100db0598 +[WICDownLinkPDU pduWithData:] (WICDownLinkPDU.m:237)

8 App 0x0000000100d89d28 -[ConnectivityRobot updateDataFromServer] (ConnectivityRobot.m:1040)

9 App 0x0000000100d895dc -[ConnectivityRobot checkDataFromServer] (ConnectivityRobot.m:945)

10 App 0x0000000100fc6ffc __46-[VACallManager vomopingRequestFromSIPOption:]_block_invoke_4 (VACallManager.m:150)

11 libdispatch.dylib 0x0000000184d05088 _dispatch_call_block_and_release + 24 (init.c:994)

12 libdispatch.dylib 0x0000000184d05048 _dispatch_client_callout + 16 (object.m:502)

13 libdispatch.dylib 0x0000000184d12378 _dispatch_root_queue_drain + 1028 (inline_internal.h:2500)

14 libdispatch.dylib 0x0000000184d11f10 _dispatch_worker_thread3 + 120 (queue.c:6104)

15 libsystem_pthread.dylib 0x0000000184fab120 _pthread_wqthread + 1268 (pthread.c:2286)

16 libsystem_pthread.dylib 0x0000000184faac20 start_wqthread + 4



the instance of creation NSDateFormatter


static NSDateFormatter *pageDateFormatter=nil;
+ (NSDateFormatter *) dateFormatter
{
    static dispatch_once_t onceToken = 0;
    dispatch_once(&onceToken, ^{
        /
            pageDateFormatter = [[NSDateFormatter alloc] init];
            [pageDateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss zzzz"];
        /
    });
    return pageDateFormatter;
}


and that function was called in GCD to retrive some information from server with Date and timestamp on server side.


    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
...
[WICPage dateFormatter];
...
    });


Any thought on this crash?


Thanks,

Replies

To start your combination of

NSDateFormatter
and fixed-format dates is troubling. See QA1480 NSDateFormatter and Internet Dates for details. However, this is unlikely to be the cause of your crash.

With regards that crash, consider this:

2 Foundation … -[NSDateFormatter dealloc] + 76 (NSDateFormatter.m:291)
3 App        … +[WICPage dateFormatter] (WICPage.m:80)

For this sequence to happen

+[WICPage dateFormatter]
must be releasing a date formatter reference. However, the code you’ve shown for
+[WICPage dateFormatter]
will never release a date formatter reference, because it only allocates the date formatter and assigns it to
pageDateFormatter
. The only way it could deallocate is if:
  • dispatch_once
    is running the block more than once
  • Someone else is putting a date formatter in

    pageDateFormatter
    , so the code in the
    dispatch_once
    is replacing an existing non-nil value

Neither seems very likely.

First things first, I’d move the

pageDateFormatter
static inside
+dateFormatter
, right next to the
onceToken
static. That’ll ensure that no one else is accidentally touching it.

Beyond that my best guess is that you have a memory management problem, so something is over-releasing some object that causes your date formatter to get released accidentally. The first step in debugging such problems is the standard memory debugging tools.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"