title: Crash when presenting a ViewController from modally presented ViewController
body: <p>In our production app we do have crash reports on presenting a <code>ViewController</code> from a modally presented `ViewController.</p>
  
  <p>We have a <code>BackgroundForegroundDelegate</code>. Whenever the app enters the background it'll present a Lockscreen on top of the current <code>ViewController</code>. </p>
  
  <p>AppDelegate:</p>
  
  <pre><code>func applicationDidEnterBackground(_ application: UIApplication) {
      backgroundForegroundDelegate?.applicationDidEnterBackground()
  }
  
  func applicationWillEnterForeground(_ application: UIApplication) {
      backgroundForegroundDelegate?.applicationWillEnterForeground()
  }
  </code></pre>
  
  <p>LockScreenViewController:</p>
  
  <pre><code>class LockScreenViewController: UIViewController {
  
      override func viewDidLoad() {
          super.viewDidLoad()
          let blurEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
          blurEffectView.frame = self.view.bounds
          blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  
          view.addSubview(blurEffectView)
          view.sendSubview(toBack: blurEffectView)
      }
  }
  </code></pre>
  
  <p>The <code>TabBarController</code>, which is the backgroundForegroundDelegate, will do the following to present that LockScreen:</p>
  
  <pre><code>lazy var lockScreenViewController = UIStoryboard(name: "Login", bundle: nil).instantiateViewController(withIdentifier: "LockScreenViewController")
  
  func applicationDidEnterBackground() {
      let visibleViewController = (self.selectedViewController as? UINavigationController)?.visibleViewController
      (visibleViewController as? BackgroundForegroundDelegate)?.applicationDidEnterBackground()
  
      lockScreenViewController.modalPresentationStyle = .overFullScreen
  
      let window = UIApplication.shared.keyWindow!
      if let modalVC = window.rootViewController?.presentedViewController {
          // HERE it does crash
          modalVC.present(lockScreenViewController, animated: false,
                          completion: nil)
      } else {
          window.rootViewController!.present(lockScreenViewController, animated: false, completion: nil)
      }
  }
  </code></pre>
  
  <p>I marked the line where the crashs occured. </p>
  
  <p>We couldn't reproduce it on the same phone with the same OS.</p>
  
  <p>What can go wrong here and how can we make it "safe"? </p>
  
  <hr>
  
  <p>Crash Report:</p>
  
  <pre><code>Hardware Model:      iPhone8,1
  Process:             MyApp [13594]
  Path:                /private/var/containers/Bundle/Application/**/MyApp.app/MyApp
  Identifier:          *hidden*
  Version:             1565 (1.2.0)
  Code Type:           ARM-64 (Native)
  Role:                Foreground
  Parent Process:      launchd [1]
  Coalition:           *hidden*
  
  
  Date/Time:           2017-09-24 14:46:10.5972 +0200
  Launch Time:         2017-09-24 14:43:55.8968 +0200
  OS Version:          iPhone OS 11.0 (15A372)
  Baseband Version:    4.00.01
  Report Version:      104
  
  Exception Type:  EXC_CRASH (SIGABRT)
  Exception Codes: 0x0000000000000000, 0x0000000000000000
  Exception Note:  EXC_CORPSE_NOTIFY
  Triggered by Thread:  0
  
  Last Exception Backtrace:
  0   CoreFoundation                  0x18261fd38 __exceptionPreprocess + 124 (NSException.m:166)
  1   libobjc.A.dylib                 0x181b34528 objc_exception_throw + 56 (objc-exception.mm:521)
  2   UIKit                           0x18bda189c -[UIViewController _presentViewController:withAnimationController:completion:] + 4828 (UIViewController.m:6945)
  3   UIKit                           0x18bda4074 __63-[UIViewController _presentViewController:animated:completion:]_block_invoke_2 + 68 (UIViewController.m:7453)
  4   UIKit                           0x18ba3ca84 +[UIView(Animation) performWithoutAnimation:] + 104 (UIView.m:11718)
  5   UIKit                           0x18bda3fc8 __63-[UIViewController _presentViewController:animated:completion:]_block_invoke + 220 (UIViewController.m:7451)
  6   UIKit                           0x18bda4488 -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 528 (UIViewController.m:7531)
  7   UIKit                           0x18bda3ea4 -[UIViewController _presentViewController:animated:completion:] + 188 (UIViewController.m:7462)
  8   UIKit                           0x18bb0e55c -[UIViewController presentViewController:animated:completion:] + 160 (UIViewController.m:7475)
  9   MyApp                   0x1041794cc _T013MyApp24TabBarControllerC29applicationDidEnterBackgroundyyF + 472 (TabBarController.swift:81)
  10  MyApp                   0x10417ace8 _T013MyApp24TabBarControllerCAA28BackgroundForegroundDelegateA2aDP019applicationDidEnterG0yyFTW + 20 (TabBarController.swift:0)
  11  MyApp                   0x104151aa0 _T013MyApp11AppDelegateC29applicationDidEnterBackgroundySo13UIApplicationCFTo + 192 (AppDelegate.swift:76)
  12  UIKit                           0x18bc9b648 __47-[UIApplication _applicationDidEnterBackground]_block_invoke + 180 (UIApplication.m:6096)
  13  UIKit                           0x18bd9e338 +[UIViewController _performWithoutDeferringTransitions:] + 128 (UIViewController.m:6538)
  14  UIKit                           0x18bc9b538 -[UIApplication _applicationDidEnterBackground] + 104 (UIApplication.m:6088)
  15  UIKit                           0x18bf2531c -[__UICanvasLifecycleMonitor_Compatability deactivateEventsOnly:withContext:forceExit:completion:] + 860 (_UICanvasLifecycleMonitor.m:375)
  16  UIKit                           0x18c68d760 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 380 (_UIApplicationCanvas.m:0)
  17  UIKit                           0x18c68d58c -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 448 (_UIApplicationCanvas.m:156)
  18  UIKit                           0x18c4099c0 __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 220 (_UICanvasLifecycleSettingsDiffAction.m:210)
  19  UIKit                           0x18c59efc8 _performActionsWithDelayForTransitionContext + 112 (_UICanvasSettingsDiffAction.m:34)
  20  UIKit                           0x18c409870 -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 252 (_UICanvasLifecycleSettingsDiffAction.m:181)
  21  UIKit                           0x18c1f3850 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 364 (_UICanvas.m:517)
  22  UIKit                           0x18c098bd8 -[UIApplicationSceneClientAgent scene:handleEvent:withCompletion:] + 468 (UIApplicationSceneClientAgent.m:77)
  23  FrontBoardServices              0x184cc221c __80-[FBSSceneImpl updater:didUpdateSettings:withDiff:transitionContext:completion:]_block_invoke.362 + 212 (FBSSceneImpl.m:571)
  24  libdispatch.dylib               0x181fa5048 _dispatch_client_callout + 16 (object.m:502)
  25  libdispatch.dylib               0x181fac6c8 _dispatch_block_invoke_direct$VARIANT$mp + 288 (queue.c:3025)
  26  FrontBoardServices              0x184cf5a04 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 36 (FBSSerialQueue.m:164)
  27  FrontBoardServices              0x184cf56a8 -[FBSSerialQueue _performNext] + 404 (FBSSerialQueue.m:196)
  28  FrontBoardServices              0x184cf5c44 -[FBSSerialQueue _performNextFromRunLoopSource] + 56 (FBSSerialQueue.m:232)
  29  CoreFoundation                  0x1825c8358 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24 (CFRunLoop.c:1982)
  30  CoreFoundation                  0x1825c82d8 __CFRunLoopDoSource0 + 88 (CFRunLoop.c:2017)
  31  CoreFoundation                  0x1825c7b60 __CFRunLoopDoSources0 + 204 (CFRunLoop.c:2053)
  32  CoreFoundation                  0x1825c5738 __CFRunLoopRun + 1048 (CFRunLoop.c:2920)
  33  CoreFoundation                  0x1824e62d8 CFRunLoopRunSpecific + 436 (CFRunLoop.c:3245)
  34  GraphicsServices                0x184377f84 GSEventRunModal + 100 (GSEvent.c:2245)
  35  UIKit                           0x18ba92880 UIApplicationMain + 208 (UIApplication.m:3948)
  36  MyApp                   0x1040c100c main + 56 (ContactsService.swift:16)
  37  libdyld.dylib                   0x18200a56c start + 4
  
  Thread 0 name:
  Thread 0 Crashed:
  0   libsystem_kernel.dylib          0x0000000182139348 __pthread_kill + 8
  1   libsystem_pthread.dylib         0x000000018224d354 pthread_kill$VARIANT$mp + 396 (pthread.c:1484)
  2   libsystem_c.dylib               0x00000001820a8fd8 abort + 140 (abort.c:91)
  3   libc++abi.dylib                 0x0000000181b0c068 abort_message + 132 (abort_message.cpp:75)
  4   libc++abi.dylib                 0x0000000181b0c210 default_terminate_handler() + 304 (cxa_default_handlers.cpp:68)
  5   libobjc.A.dylib                 0x0000000181b34810 _objc_terminate() + 124 (objc-exception.mm:657)
  6   libc++abi.dylib                 0x0000000181b2454c std::__terminate(void (*)()) + 16 (cxa_handlers.cpp:66)
  7   libc++abi.dylib                 0x0000000181b245b8 std::terminate() + 60 (cxa_handlers.cpp:97)
  8   libdispatch.dylib               0x0000000181fa505c _dispatch_client_callout + 36 (object.m:505)
  9   libdispatch.dylib               0x0000000181fac6c8 _dispatch_block_invoke_direct$VARIANT$mp + 288 (queue.c:3025)
  10  FrontBoardServices              0x0000000184cf5a04 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 36 (FBSSerialQueue.m:164)
  11  FrontBoardServices              0x0000000184cf56a8 -[FBSSerialQueue _performNext] + 404 (FBSSerialQueue.m:196)
  12  FrontBoardServices              0x0000000184cf5c44 -[FBSSerialQueue _performNextFromRunLoopSource] + 56 (FBSSerialQueue.m:232)
  13  CoreFoundation                  0x00000001825c8358 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24 (CFRunLoop.c:1982)
  14  CoreFoundation                  0x00000001825c82d8 __CFRunLoopDoSource0 + 88 (CFRunLoop.c:2017)
  15  CoreFoundation                  0x00000001825c7b60 __CFRunLoopDoSources0 + 204 (CFRunLoop.c:2053)
  16  CoreFoundation                  0x00000001825c5738 __CFRunLoopRun + 1048 (CFRunLoop.c:2920)
  17  CoreFoundation                  0x00000001824e62d8 CFRunLoopRunSpecific + 436 (CFRunLoop.c:3245)
  18  GraphicsServices                0x0000000184377f84 GSEventRunModal + 100 (GSEvent.c:2245)
  19  UIKit                           0x000000018ba92880 UIApplicationMain + 208 (UIApplication.m:3948)
  20  MyApp                   0x00000001040c100c main + 56 (ContactsService.swift:16)
  21  libdyld.dylib                   0x000000018200a56c start + 4
  
  Thread 1 name:
  Thread 1:
  0   libsystem_kernel.dylib          0x0000000182118bc4 mach_msg_trap + 8
  1   libsystem_kernel.dylib          0x0000000182118a3c mach_msg + 72 (mach_msg.c:103)
  2   CoreFoundation                  0x00000001825c7ce4 __CFRunLoopServiceMachPort + 196 (CFRunLoop.c:2613)
  3   CoreFoundation                  0x00000001825c58b0 __CFRunLoopRun + 1424 (CFRunLoop.c:2969)
  4   CoreFoundation                  0x00000001824e62d8 CFRunLoopRunSpecific + 436 (CFRunLoop.c:3245)
  5   Foundation                      0x0000000182f0e6e4 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 304 (NSRunLoop.m:367)
  6   Foundation                      0x0000000182f2dafc -[NSRunLoop(NSRunLoop) runUntilDate:] + 96 (NSRunLoop.m:411)
  7   UIKit                           0x000000018c5de02c -[UIEventFetcher threadMain] + 136 (UIEventFetcher.m:422)
  8   Foundation                      0x000000018300f860 __NSThread__start__ + 996 (NSThread.m:1181)
  9   libsystem_pthread.dylib         0x000000018224c32c _pthread_body + 308 (pthread.c:740)
  10  libsystem_pthread.dylib         0x000000018224c1f8 _pthread_start + 312 (pthread.c:799)
  11  libsystem_pthread.dylib         0x000000018224ac38 thread_start + 4
  
  Thread 2 name:
  Thread 2:
  0   libsystem_kernel.dylib          0x0000000182118bc4 mach_msg_trap + 8
  1   libsystem_kernel.dylib          0x0000000182118a3c mach_msg + 72 (mach_msg.c:103)
  2   CoreFoundation                  0x00000001825c7ce4 __CFRunLoopServiceMachPort + 196 (CFRunLoop.c:2613)
  3   CoreFoundation                  0x00000001825c58b0 __CFRunLoopRun + 1424 (CFRunLoop.c:2969)
  4   CoreFoundation                  0x00000001824e62d8 CFRunLoopRunSpecific + 436 (CFRunLoop.c:3245)
  5   Foundation                      0x0000000182f0e6e4 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 304 (NSRunLoop.m:367)
  6   Foundation                      0x0000000182f6062c -[NSRunLoop(NSRunLoop) run] + 88 (NSRunLoop.m:389)
  7   MyApp                   0x00000001041c7330 +[GAI threadMain:] + 64 (GAI.m:228)
  8   Foundation                      0x000000018300f860 __NSThread__start__ + 996 (NSThread.m:1181)
  9   libsystem_pthread.dylib         0x000000018224c32c _pthread_body + 308 (pthread.c:740)
  10  libsystem_pthread.dylib         0x000000018224c1f8 _pthread_start + 312 (pthread.c:799)
  11  libsystem_pthread.dylib         0x000000018224ac38 thread_start + 4
  
  Thread 3:
  0   libsystem_kernel.dylib          0x0000000182139dbc __workq_kernreturn + 8
  1   libsystem_pthread.dylib         0x000000018224afb0 _pthread_wqthread + 884 (pthread.c:0)
  2   libsystem_pthread.dylib         0x000000018224ac30 start_wqthread + 4
  
  Thread 4:
  0   libsystem_pthread.dylib         0x000000018224ac2c start_wqthread + 0 (pthread.c:2447)
  
  Thread 5:
  0   libsystem_pthread.dylib         0x000000018224ac2c start_wqthread + 0 (pthread.c:2447)
  
  Thread 6 name:
  Thread 6:
  0   libsystem_kernel.dylib          0x0000000182118bc4 mach_msg_trap + 8
  1   libsystem_kernel.dylib          0x0000000182118a3c mach_msg + 72 (mach_msg.c:103)
  2   libdispatch.dylib               0x0000000181fbacf4 _dispatch_mach_send_and_wait_for_reply + 644 (mach.c:831)
  3   libdispatch.dylib               0x0000000181fbb19c dispatch_mach_send_with_result_and_wait_for_reply$VARIANT$mp + 56 (mach.c:1958)
  4   libxpc.dylib                    0x00000001822879cc xpc_connection_send_message_with_reply_sync + 196 (connection.c:802)
  5   BaseBoard                       0x0000000184aed634 -[BSXPCMessage sendSynchronouslyToConnection:error:] + 76 (BSXPCMessage.m:189)
  6   BaseBoard                       0x0000000184ae9dac -[BSBaseXPCClient _sendMessage:withReplyHandler:waitForReply:waitDuration:] + 320 (BSBaseXPCClient.m:233)
  7   AssertionServices               0x0000000184b3cc10 -[BKSAssertionClientMultiplexer sendEvent:withAssertionType:forEventType:responseHandler:] + 272 (BKSAssertionClientMultiplexer.m:87)
  8   AssertionServices               0x0000000184b36a98 -[BKSProcessAssertionClient sendCreateEvent:error:] + 116 (BKSProcessAssertionClient.m:53)
  9   AssertionServices               0x0000000184b3a4c8 -[BKSAssertion _clientQueue_acquireAssertion] + 136 (BKSAssertion.m:293)
  10  AssertionServices               0x0000000184b3a0fc __45-[BKSAssertion _registerAssertionAndAcquire:]_block_invoke + 80 (BKSAssertion.m:235)
  11  libdispatch.dylib               0x0000000181fa5088 _dispatch_call_block_and_release + 24 (init.c:994)
  12  libdispatch.dylib               0x0000000181fa5048 _dispatch_client_callout + 16 (object.m:502)
  13  libdispatch.dylib               0x0000000181faee48 _dispatch_queue_serial_drain$VARIANT$mp + 528 (inline_internal.h:2500)
  14  libdispatch.dylib               0x0000000181faf7d8 _dispatch_queue_invoke$VARIANT$mp + 340 (queue.c:5302)
  15  libdispatch.dylib               0x0000000181fb0200 _dispatch_root_queue_drain_deferred_wlh$VARIANT$mp + 400 (queue.c:5920)
  16  libdispatch.dylib               0x0000000181fb84a0 _dispatch_workloop_worker_thread$VARIANT$mp + 644 (source.c:2515)
  17  libsystem_pthread.dylib         0x000000018224afe0 _pthread_wqthread + 932 (pthread.c:2207)
  18  libsystem_pthread.dylib         0x000000018224ac30 start_wqthread + 4
  
  Thread 7:
  0   libsystem_pthread.dylib         0x000000018224ac2c start_wqthread + 0 (pthread.c:2447)
  
  Thread 8 name:
  Thread 8:
  0   libsystem_kernel.dylib          0x0000000182118bc4 mach_msg_trap + 8
  1   libsystem_kernel.dylib          0x0000000182118a3c mach_msg + 72 (mach_msg.c:103)
  2   CoreFoundation                  0x00000001825c7ce4 __CFRunLoopServiceMachPort + 196 (CFRunLoop.c:2613)
  3   CoreFoundation                  0x00000001825c58b0 __CFRunLoopRun + 1424 (CFRunLoop.c:2969)
  4   CoreFoundation                  0x00000001824e62d8 CFRunLoopRunSpecific + 436 (CFRunLoop.c:3245)
  5   CFNetwork                       0x0000000182c4fb40 +[NSURLConnection(Loader) _resourceLoadLoop:] + 404 (NSURLConnection.mm:367)
  6   Foundation                      0x000000018300f860 __NSThread__start__ + 996 (NSThread.m:1181)
  7   libsystem_pthread.dylib         0x000000018224c32c _pthread_body + 308 (pthread.c:740)
  8   libsystem_pthread.dylib         0x000000018224c1f8 _pthread_start + 312 (pthread.c:799)
  9   libsystem_pthread.dylib         0x000000018224ac38 thread_start + 4
  
  Thread 0 crashed with ARM Thread State (64-bit):
      x0: 0x0000000000000000   x1: 0x0000000000000000   x2: 0x0000000000000000   x3: 0x00000001c42ea5b7
      x4: 0x0000000181b29afd   x5: 0x000000016bd425a0   x6: 0x000000000000006e   x7: 0xffffffffffffffec
      x8: 0x0000000008000000   x9: 0x0000000004000000  x10: 0x000000018225163c  x11: 0x0000000000000003
     x12: 0xffffffffffffffff  x13: 0x0000000000000001  x14: 0x00000001820bf557  x15: 0x0000000000000010
     x16: 0x0000000000000148  x17: 0x00000000ffffffff  x18: 0x0000000000000000  x19: 0x0000000000000006
     x20: 0x00000001b2fe3b80  x21: 0x000000016bd425a0  x22: 0x0000000000000303  x23: 0x00000001b2fe3c60
     x24: 0x0000000000002403  x25: 0x0000000000002303  x26: 0x0000000000000001  x27: 0x0000000000000000
     x28: 0x00000001c41fc808   fp: 0x000000016bd42500   lr: 0x000000018224d354
      sp: 0x000000016bd424d0   pc: 0x0000000182139348 cpsr: 0x00000000
  </code></pre>