iPhone App Crashing when Coming From Background After Expiration Handler Ran

1

I'm having some issues with an iPhone app crashing when it comes from background, it is crashing in two ways.

Here is one stack trace:

    Hardware Model:  iPhone4,1
    Process:         MyApp [11247]
    Identifier:      MyApp
    Code Type:       ARM (Native)
    Parent Process:  launchd [1]
    OS Version:      iPhone OS 5.0.1 (9A406)
    Report Version:  104

    Exception Type:  EXC_CRASH (SIGSEGV)
    Exception Codes: 0x00000000, 0x00000000
    Crashed Thread:  0

    Thread 0 name:  Dispatch queue: com.apple.main-thread
    Thread 0 Crashed:
    0   libsystem_kernel.dylib          0x30938010 0x30937000 + 4112
    1   libsystem_kernel.dylib          0x30938206 0x30937000 + 4614
    2   liblaunch.dylib                 0x3717d114 0x37177000 + 24852
    3   liblaunch.dylib                 0x3717af4a 0x37177000 + 16202
    4   liblaunch.dylib                 0x3717affa 0x37177000 + 16378
    5   CoreTelephony                   0x37c590aa 0x37c42000 + 94378
    6   IMAVCore                        0x3713d15a 0x3711d000 + 131418
    7   IMAVCore                        0x3713d522 0x3711d000 + 132386
    8   CoreFoundation                  0x372396bc 0x371f8000 + 267964
    9   CoreTelephony                   0x37c68f3e 0x37c42000 + 159550
    10  CoreTelephony                   0x37c5929c 0x37c42000 + 94876
    11  CoreFoundation                  0x3727b0c4 0x371f8000 + 536772
    12  libdispatch.dylib               0x33ac2d4e 0x33ac2000 + 3406
    13  libdispatch.dylib               0x33ac4f74 0x33ac2000 + 12148
    14  CoreFoundation                  0x372842d6 0x371f8000 + 574166
    15  CoreFoundation                  0x372074d6 0x371f8000 + 62678
    16  CoreFoundation                  0x3720739e 0x371f8000 + 62366
    17  GraphicsServices                0x313bdfc6 0x313ba000 + 16326
    18  UIKit                           0x3287973c 0x32848000 + 202556
    19  MyApp                           0x00003eae main (main.m:16)
    20  MyApp                           0x00003e6c start + 32

    Thread 1 name:  Dispatch queue: com.apple.libdispatch-manager
    Thread 1:
    0   libsystem_kernel.dylib          0x309383b4 0x30937000 + 5044
    1   libdispatch.dylib               0x33ac5f74 0x33ac2000 + 16244
    2   libdispatch.dylib               0x33ac5c92 0x33ac2000 + 15506

    Thread 2 name:  WebThread
    Thread 2:
    0   libsystem_kernel.dylib          0x30938010 0x30937000 + 4112
    1   libsystem_kernel.dylib          0x30938206 0x30937000 + 4614
    2   CoreFoundation                  0x3728541c 0x371f8000 + 578588
    3   CoreFoundation                  0x37284154 0x371f8000 + 573780
    4   CoreFoundation                  0x372074d6 0x371f8000 + 62678
    5   CoreFoundation                  0x3720739e 0x371f8000 + 62366
    6   WebCore                         0x3305e128 0x32fb6000 + 688424
    7   libsystem_c.dylib               0x31f24c16 0x31f15000 + 64534
    8   libsystem_c.dylib               0x31f24ad0 0x31f15000 + 64208

    Thread 3:
    0   libsystem_kernel.dylib          0x30948cd4 0x30937000 + 72916
    1   libsystem_c.dylib               0x31f1f30a 0x31f15000 + 41738
    2   libsystem_c.dylib               0x31f1f09c 0x31f15000 + 41116

    Thread 4:
    0   libsystem_kernel.dylib          0x30938010 0x30937000 + 4112
    1   libsystem_kernel.dylib          0x30938206 0x30937000 + 4614
    2   CoreFoundation                  0x3728541c 0x371f8000 + 578588
    3   CoreFoundation                  0x37284154 0x371f8000 + 573780
    4   CoreFoundation                  0x372074d6 0x371f8000 + 62678
    5   CoreFoundation                  0x3720739e 0x371f8000 + 62366
    6   Foundation                      0x31c8ab7e 0x31c86000 + 19326
    7   MyApp                       0x00057e7c +[XMPPStream xmppThreadMain] (XMPPStream.m:4089)
    8   Foundation                      0x31c96a8a 0x31c86000 + 68234
    9   Foundation                      0x31d2a59a 0x31c86000 + 673178
    10  libsystem_c.dylib               0x31f24c16 0x31f15000 + 64534
    11  libsystem_c.dylib               0x31f24ad0 0x31f15000 + 64208

    Thread 5 name:  AURemoteIO::IOThread
    Thread 5:
    0   libsystem_kernel.dylib          0x30938010 0x30937000 + 4112
    1   libsystem_kernel.dylib          0x30938206 0x30937000 + 4614
    2   AudioToolbox                    0x373e048a 0x37321000 + 783498
    3   AudioToolbox                    0x373e40ae 0x37321000 + 798894
    4   AudioToolbox                    0x37322aac 0x37321000 + 6828
    5   libsystem_c.dylib               0x31f24c16 0x31f15000 + 64534
    6   libsystem_c.dylib               0x31f24ad0 0x31f15000 + 64208

    Thread 6 name:  com.apple.NSURLConnectionLoader
    Thread 6:
    0   libsystem_kernel.dylib          0x30938010 0x30937000 + 4112
    1   libsystem_kernel.dylib          0x30938206 0x30937000 + 4614
    2   CoreFoundation                  0x3728541c 0x371f8000 + 578588
    3   CoreFoundation                  0x37284154 0x371f8000 + 573780
    4   CoreFoundation                  0x372074d6 0x371f8000 + 62678
    5   CoreFoundation                  0x3720739e 0x371f8000 + 62366
    6   Foundation                      0x31c96bc2 0x31c86000 + 68546
    7   Foundation                      0x31c96a8a 0x31c86000 + 68234
    8   Foundation                      0x31d2a59a 0x31c86000 + 673178
    9   libsystem_c.dylib               0x31f24c16 0x31f15000 + 64534
    10  libsystem_c.dylib               0x31f24ad0 0x31f15000 + 64208

    Thread 7:
    0   libsystem_kernel.dylib          0x30938010 0x30937000 + 4112
    1   libsystem_kernel.dylib          0x30938206 0x30937000 + 4614
    2   CoreFoundation                  0x3728541c 0x371f8000 + 578588
    3   CoreFoundation                  0x37284154 0x371f8000 + 573780
    4   CoreFoundation                  0x372074d6 0x371f8000 + 62678
    5   CoreFoundation                  0x3720739e 0x371f8000 + 62366
    6   Foundation                      0x31c8ab7e 0x31c86000 + 19326
    7   Foundation                      0x31ca452c 0x31c86000 + 124204
    8   MyApp                           0x00070b28 +[GCDAsyncSocket listenerThread] (GCDAsyncSocket.m:6283)
    9   Foundation                      0x31c96a8a 0x31c86000 + 68234
    10  Foundation                      0x31d2a59a 0x31c86000 + 673178
    11  libsystem_c.dylib               0x31f24c16 0x31f15000 + 64534
    12  libsystem_c.dylib               0x31f24ad0 0x31f15000 + 64208

    Thread 8 name:  com.apple.CFSocket.private
    Thread 8:
    0   libsystem_kernel.dylib          0x30948570 0x30937000 + 71024
    1   CoreFoundation                  0x3728966a 0x371f8000 + 595562
    2   libsystem_c.dylib               0x31f24c16 0x31f15000 + 64534
    3   libsystem_c.dylib               0x31f24ad0 0x31f15000 + 64208

    Thread 9 name:  Dispatch queue: cocoa.lumberjack
    Thread 9:
    0   libsystem_kernel.dylib          0x30938060 0x30937000 + 4192
    1   libdispatch.dylib               0x33ac6472 0x33ac2000 + 17522
    2   libdispatch.dylib               0x33ac63d2 0x33ac2000 + 17362
    3   MyApp                           0x0007a3e2 +[DDLog lt_log:] (DDLog.m:922)
    4   MyApp                           0x00079158 __40+[DDLog queueLogMessage:asynchronously:]_block_invoke_0 (DDLog.m:449)
    5   libdispatch.dylib               0x33ac2d4e 0x33ac2000 + 3406
    6   libdispatch.dylib               0x33ac4dc0 0x33ac2000 + 11712
    7   libdispatch.dylib               0x33ac4c56 0x33ac2000 + 11350
    8   libdispatch.dylib               0x33ac5860 0x33ac2000 + 14432
    9   libsystem_c.dylib               0x31f1f1c8 0x31f15000 + 41416
    10  libsystem_c.dylib               0x31f1f09c 0x31f15000 + 41116

    Thread 10:
    0   libsystem_kernel.dylib          0x30948cd4 0x30937000 + 72916
    1   libsystem_c.dylib               0x31f1f30a 0x31f15000 + 41738
    2   libsystem_c.dylib               0x31f1f09c 0x31f15000 + 41116

    Thread 11:
    0   libsystem_kernel.dylib          0x30948cd4 0x30937000 + 72916
    1   libsystem_c.dylib               0x31f1f30a 0x31f15000 + 41738
    2   libsystem_c.dylib               0x31f1f09c 0x31f15000 + 41116

    Thread 12 name:  Dispatch queue: cocoa.lumberjack.fileLogger
    Thread 12:
    0   CoreFoundation                  0x3723875a 0x371f8000 + 264026
    1   Foundation                      0x31cb0722 0x31c86000 + 173858
    2   Foundation                      0x31cb069a 0x31c86000 + 173722
    3   MyApp                           0x001d8510 -[MyCustomFormatter formatLogMessage:] (MyAppDelegate.m:101)
    4   MyApp                           0x0007782c -[DDFileLogger logMessage:] (DDFileLogger.m:986)
    5   MyApp                           0x0007a79e __16+[DDLog lt_log:]_block_invoke_0 (DDLog.m:916)
    6   libdispatch.dylib               0x33ac2d4e 0x33ac2000 + 3406
    7   libdispatch.dylib               0x33ac4dc0 0x33ac2000 + 11712
    8   libdispatch.dylib               0x33ac4c56 0x33ac2000 + 11350
    9   libdispatch.dylib               0x33ac5860 0x33ac2000 + 14432
    10  libsystem_c.dylib               0x31f1f1c8 0x31f15000 + 41416
    11  libsystem_c.dylib               0x31f1f09c 0x31f15000 + 41116

    Thread 13:
    0   libsystem_kernel.dylib          0x30948cd4 0x30937000 + 72916
    1   libsystem_c.dylib               0x31f1f30a 0x31f15000 + 41738
    2   libsystem_c.dylib               0x31f1f09c 0x31f15000 + 41116

    Thread 14:
    0   libsystem_kernel.dylib          0x30948cd4 0x30937000 + 72916
    1   libsystem_c.dylib               0x31f1f30a 0x31f15000 + 41738
    2   libsystem_c.dylib               0x31f1f09c 0x31f15000 + 41116

    Thread 0 crashed with ARM Thread State:
        r0: 0x00000000    r1: 0x03000003      r2: 0x000000bc      r3: 0x00000068
        r4: 0x00000107    r5: 0x00000000      r6: 0x00000000      r7: 0x2fdfe3c8
        r8: 0x00000000    r9: 0x00a80df8     r10: 0x03000003     r11: 0x00000000
        ip: 0xffffffe1    sp: 0x2fdfe38c      lr: 0x3093820d      pc: 0x30938010
      cpsr: 0x200f0010

The other is similar and crashes in Thread 12 in -[MyCustomFormatter formatLogMessage:] (MyAppDelegate.m:101)

Here is the code of - (NSString *)formatLogMessage:(DDLogMessage *)logMessage:

    NSString *logLevel;
    switch (logMessage->logFlag) {
      case LOG_FLAG_ERROR : logLevel = @"[E]"; break;
      case LOG_FLAG_WARN  : logLevel = @"[W]"; break;
      case LOG_FLAG_INFO  : logLevel = @"[I]"; break;
      default             : logLevel = @"[V]"; break;
    }
    /*line 100*/
    /*line 101*/ NSString *dateAndTime = [dateFormatter stringFromDate:(logMessage->timestamp)];
    /*line 102*/
    NSString *logMsg = logMessage->logMsg;

    NSString *fileName = [NSString stringWithCString:logMessage->file encoding:[NSString defaultCStringEncoding]];

    NSString *function = [NSString stringWithCString:logMessage->function encoding:[NSString defaultCStringEncoding]]; 

    return [NSString stringWithFormat:@"%@ - %@ (%@ - %@) | %@\n", logLevel, dateAndTime, fileName, function, logMsg];

And the dateFormatter is initialized in MyCustomFormatter init:

    - (id)init
    {
        if((self = [super init]))
        {
            self.dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
            [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
            [dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss:SSS"];
        }
        return self;
    }

And its interface is the following:

    @interface MyCustomFormatter : NSObject <DDLogFormatter>
    {       
        NSDateFormatter *dateFormatter;
    }

    @property (nonatomic, retain) NSDateFormatter *dateFormatter;

    @end

My code in App Delegate - (void)applicationWillResignActive:(UIApplication *)application looks like the following:

    self.bgTask = [app beginBackgroundTaskWithExpirationHandler:^{

    DDLogInfo(@"Ending bg task in expiration handler");
    //TODO: remove after tests
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:66];

    [[XMPP instance] disconnect];

    [app endBackgroundTask:self.bgTask];

    self.bgTask = UIBackgroundTaskInvalid;

    }];


    // Start the long-running task and return immediately.

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // Do the work associated with the task.

        sleep(600);

        if(self.bgTask != UIBackgroundTaskInvalid){
            //TODO: remove after tests
            [[UIApplication sharedApplication] setApplicationIconBadgeNumber:99];

            DDLogInfo(@"Disconnecting from XMPP in bg task after waited in bg");
            [[XMPP instance] disconnect];

            [app endBackgroundTask:self.bgTask];

            self.bgTask = UIBackgroundTaskInvalid;
        }

    });

And the - (void)applicationWillEnterForeground:(UIApplication *)application:

    DDLogVerbose(@"ApplicationWillEnterForeground:");
    if(self.bgTask != UIBackgroundTaskInvalid){
      [[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
      self.bgTask = UIBackgroundTaskInvalid;
    }
    else {
      [[XMPP instance] connect];
    }

I've set up the badges in order to know if the app is still running or not, and how it stopped, and noticed the app crashes when it is opening from background with the badge 66 only.

Any help would be appreciated.

Thanks in advance.

iphone
objective-c
ios
background
crash
asked on Stack Overflow Feb 14, 2012 by ZeCodea

1 Answer

1

It seems the problem was with the date formatter not being thread safe was being used simultaneously by my three loggers, see SO question

answered on Stack Overflow Feb 23, 2012 by ZeCodea • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0