title: Intermittent iPad app crash using Swift
body: <p>I have been seeing the following crash intermittently in my app. Following is the crash log for the crash. I am not able to figure out the cause for this crash. I would really appreciate if anyone could help me with the reason behind this crash.</p>
  
  <p>CRASHLOG:</p>
  
  <blockquote>
    <p>Exception Type:  EXC_BREAKPOINT (SIGTRAP)
    Exception Codes: 0x0000000000000001, 0x000000000000defe
    Triggered by Thread:  5</p>
  </blockquote>
  
  <pre><code>Thread 0 name:  Dispatch queue: com.apple.main-thread
  Thread 0:
  0   libsystem_kernel.dylib          0x34515648 syscall_thread_switch + 8
  1   libsystem_platform.dylib        0x345a2646 _OSSpinLockLockSlow$VARIANT$mp + 42
  2   CFNetwork                       0x25df2f7e _BrowserCancel(__CFNetServiceBrowser*) + 22
  3   CoreFoundation                  0x2631f804 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 12
  4   CoreFoundation                  0x2631ec16 __CFRunLoopDoSources0 + 218
  5   CoreFoundation                  0x2631d294 __CFRunLoopRun + 764
  6   CoreFoundation                  0x2626adac CFRunLoopRunSpecific + 472
  7   CoreFoundation                  0x2626abbe CFRunLoopRunInMode + 102
  8   GraphicsServices                0x2d5dc04c GSEventRunModal + 132
  9   UIKit                           0x29836a2c UIApplicationMain + 1436
  10  Xavier                          0x000c85c4 0x6c000 + 378308
  11  Xavier                          0x000c8720 0x6c000 + 378656
  12  libdyld.dylib                   0x34463aac start + 0
  
  Thread 1 name:  Dispatch queue: com.apple.libdispatch-manager
  Thread 1:
  0   libsystem_kernel.dylib          0x345152c8 kevent64 + 24
  1   libdispatch.dylib               0x34437ec4 _dispatch_mgr_invoke + 276
  2   libdispatch.dylib               0x34437bf6 _dispatch_mgr_thread$VARIANT$mp + 34
  
  Thread 2 name:  com.apple.CFSocket.private
  Thread 2:
  0   libsystem_kernel.dylib          0x3452908c __select + 20
  1   CoreFoundation                  0x2632354e __CFSocketManager + 486
  2   libsystem_pthread.dylib         0x345a7e64 _pthread_body + 136
  3   libsystem_pthread.dylib         0x345a7dd6 _pthread_start + 114
  4   libsystem_pthread.dylib         0x345a5b80 thread_start + 4
  
  Thread 3 name:  com.apple.NSURLConnectionLoader
  Thread 3:
  0   libsystem_kernel.dylib          0x34515518 mach_msg_trap + 20
  1   libsystem_kernel.dylib          0x3451530c mach_msg + 36
  2   CoreFoundation                  0x2631edc6 __CFRunLoopServiceMachPort + 142
  3   CoreFoundation                  0x2631d38c __CFRunLoopRun + 1012
  4   CoreFoundation                  0x2626adac CFRunLoopRunSpecific + 472
  5   CoreFoundation                  0x2626abbe CFRunLoopRunInMode + 102
  6   CFNetwork                       0x25e231fa +[NSURLConnection(Loader) _resourceLoadLoop:] +   482
  7   Foundation                      0x270671b6 __NSThread__main__ + 1114
  8   libsystem_pthread.dylib         0x345a7e64 _pthread_body + 136
  9   libsystem_pthread.dylib         0x345a7dd6 _pthread_start + 114
  10  libsystem_pthread.dylib         0x345a5b80 thread_start + 4
  
  Thread 4:
  0   libsystem_kernel.dylib          0x34515518 mach_msg_trap + 20
  1   libsystem_kernel.dylib          0x3451530c mach_msg + 36
  2   CoreFoundation                  0x2631edc6 __CFRunLoopServiceMachPort + 142
  3   CoreFoundation                  0x2631d38c __CFRunLoopRun + 1012
  4   CoreFoundation                  0x2626adac CFRunLoopRunSpecific + 472
  5   CoreFoundation                  0x262b46c6 CFRunLoopRun + 94
  6   CoreMotion                      0x26b6bb7a 0x26b2d000 + 256890
  7   libsystem_pthread.dylib         0x345a7e64 _pthread_body + 136
  8   libsystem_pthread.dylib         0x345a7dd6 _pthread_start + 114
  9   libsystem_pthread.dylib         0x345a5b80 thread_start + 4
  
  Thread 5 name:  Dispatch queue: com.apple.MCSession.callbackQueue
  Thread 5 Crashed:
  0   libswiftCore.dylib              0x0049b69c 0x304000 + 1668764
  1   Xavier                          0x000d4af8 0x6c000 + 428792
  2   Xavier                          0x000d5008 0x6c000 + 430088
  3   MultipeerConnectivity           0x2824ba26 __58-[MCSession syncHandleNetworkEvent:pid:freeEventWhenDone:]_block_invoke617 + 114
  4   libdispatch.dylib               0x34428610 _dispatch_call_block_and_release + 8
  5   libdispatch.dylib               0x34432714 _dispatch_queue_drain$VARIANT$mp + 944
  6   libdispatch.dylib               0x344321e4 _dispatch_queue_invoke$VARIANT$mp + 80
  7   libdispatch.dylib               0x34434156 _dispatch_root_queue_drain + 310
  8   libdispatch.dylib               0x3443527a _dispatch_worker_thread3 + 102
  9   libsystem_pthread.dylib         0x345a5e22 _pthread_wqthread + 666
  10  libsystem_pthread.dylib         0x345a5b74 start_wqthread + 4
  
  Thread 6:
  0   libsystem_kernel.dylib          0x345299cc __workq_kernreturn + 8
  1   libsystem_pthread.dylib         0x345a5e9c _pthread_wqthread + 788
  2   libsystem_pthread.dylib         0x345a5b74 start_wqthread + 4
  
  Thread 7:
  0   libsystem_kernel.dylib          0x345299cc __workq_kernreturn + 8
  1   libsystem_pthread.dylib         0x345a5e9c _pthread_wqthread + 788
  2   libsystem_pthread.dylib         0x345a5b74 start_wqthread + 4
  
  Thread 8:
  0   libsystem_kernel.dylib          0x345299cc __workq_kernreturn + 8
  1   libsystem_pthread.dylib         0x345a5e9c _pthread_wqthread + 788
  2   libsystem_pthread.dylib         0x345a5b74 start_wqthread + 4
  
  Thread 9:
  0   libsystem_kernel.dylib          0x345299cc __workq_kernreturn + 8
  1   libsystem_pthread.dylib         0x345a5e9c _pthread_wqthread + 788
  2   libsystem_pthread.dylib         0x345a5b74 start_wqthread + 4
  
  Thread 10 name:  com.apple.gamekitservices.gcksession.recvproc
  Thread 10:
  0   libsystem_kernel.dylib          0x3452908c __select + 20
  1   MultipeerConnectivity           0x2825f8cc gckSessionRecvProc + 5252
  2   libsystem_pthread.dylib         0x345a7e64 _pthread_body + 136
  3   libsystem_pthread.dylib         0x345a7dd6 _pthread_start + 114
  4   libsystem_pthread.dylib         0x345a5b80 thread_start + 4
  
  Thread 11 name:  com.apple.gamekitservices.gcksession.sendproc
  Thread 11:
  0   libsystem_kernel.dylib          0x3452908c __select + 20
  1   MultipeerConnectivity           0x2825fe68 gckSessionSendProc + 268
  2   libsystem_pthread.dylib         0x345a7e64 _pthread_body + 136
  3   libsystem_pthread.dylib         0x345a7dd6 _pthread_start + 114
  4   libsystem_pthread.dylib         0x345a5b80 thread_start + 4
  
  Thread 12 name:  com.apple.gamekitservices.eventcallback.eventcbproc
  Thread 12:
  0   libsystem_kernel.dylib          0x34528b38 __psynch_cvwait + 24
  1   libsystem_pthread.dylib         0x345a73dc _pthread_cond_wait + 516
  2   libsystem_pthread.dylib         0x345a82ac pthread_cond_wait + 36
  3   MultipeerConnectivity           0x28279a4c EventCBProc + 76
  4   libsystem_pthread.dylib         0x345a7e64 _pthread_body + 136
  5   libsystem_pthread.dylib         0x345a7dd6 _pthread_start + 114
  6   libsystem_pthread.dylib         0x345a5b80 thread_start + 4
  
  Thread 13:
  0   libsystem_kernel.dylib          0x34528b38 __psynch_cvwait + 24
  1   libsystem_pthread.dylib         0x345a7416 _pthread_cond_wait + 574
  2   libsystem_pthread.dylib         0x345a71d0 pthread_cond_timedwait_relative_np + 12
  3   MultipeerConnectivity           0x2825de5c gckSessionRecvUDPPacketWithTimeout + 252
  4   ICE                             0x2d297234 ICERecvUDPPacketWithTimeout + 340
  5   ICE                             0x2d29eed2 RecvAndProcess + 310
  6   ICE                             0x2d2a2d1e ConnectivityCheckProc + 894
  7   libsystem_pthread.dylib         0x345a7e64 _pthread_body + 136
  8   libsystem_pthread.dylib         0x345a7dd6 _pthread_start + 114
  9   libsystem_pthread.dylib         0x345a5b80 thread_start + 4
  
  Thread 5 crashed with ARM Thread State (32-bit):
  r0: 0x00000000    r1: 0x29f6be18      r2: 0x34e7b7e4      r3: 0x00000000
  r4: 0x17de21f0    r5: 0x2d34620a      r6: 0x17de60d0      r7: 0x00852bc8
  r8: 0x17de60d0    r9: 0x18a6a4c0     r10: 0x36c40660     r11: 0x00000000
  ip: 0x33ee2531    sp: 0x00852bc4      lr: 0x33ee25c3      pc: 0x0049b69c
  cpsr: 0x60000030
  </code></pre>
  
  <p>My breakpoint hits this line before it crashes:</p>
  
  <pre><code>let rootVC: UINavigationController = UIApplication.sharedApplication().keyWindow.rootViewController as UINavigationController
  </code></pre>
  
  <p>Following is the whole method that contains this line:</p>
  
  <pre><code>     func session(session: MCSession!, didReceiveData data: NSData!, fromPeer peerID: MCPeerID!) {
  
      //Check to see what kind of data is received
      var dataReceived:AnyObject = NSKeyedUnarchiver.unarchiveObjectWithData(data)!;
  
      if let newData = dataReceived as? String {
          let rootVC: UINavigationController = UIApplication.sharedApplication().keyWindow.rootViewController as UINavigationController;
          let topVC = rootVC.topViewController;
  
          if let block = self.commandResolver[newData] {
              block(topVC);
              return;
          } else {
              println("Command resolver returns nil block");
          }
      } else if let newData = dataReceived as? NSDictionary {
          let rootVC: UINavigationController = UIApplication.sharedApplication().keyWindow.rootViewController as UINavigationController;
           let topVC = rootVC.topViewController as ViewController;
           topVC.connected(true, withGroupName: newData["GroupName"] as String!);
      } else {
          println("Data is not a String");
      }
  }
  </code></pre>
  
  <p>My window.rootViewController is UINavigationController so it should not be nil if that is the problem. Can it be though?</p>
  
  <p>I would really appreciate your help guys.</p>