title: Occasional black screen after resuming iPhone app
body: <p>While testing a new build of our iPhone app, we're occasionally seeing the app window go completely black when resuming the app sometime after applicationWillEnterForeground.  The only way to make the app work again is to close it completely and then restart the app.  We have not been able to identify steps to consistently reproduce.  This issue seems to happen at random, but only when going back into the app after it has been put in the background.  Sometimes it happens after a few seconds, sometimes it won't happen for over a week.</p>
  
  <p>Here are the threads in the main view during normal operation:</p>
  
  <p><img src="https://i.stack.imgur.com/ydBpU.png" alt="threadsDuringNormalOperation"></p>
  
  <p>Here are the threads when this issue happens and I pause the debugger:</p>
  
  <p><img src="https://i.stack.imgur.com/LYr2E.png" alt="threadsDuringBlackScreen"></p>
  
  <h1>Things I've tried</h1>
  
  <h3><i>Logging everything when the app resumes</i></h3>
  
  <p>Nothing appears to be out of order when the app resumes.  Furthermore, none of my code is executed after <code>applicationDidBecomeActive</code> until the home button is pressed.</p>
  
  <pre><code>2013-05-15 22:22:23 AppDelegate::applicationDidBecomeActive
  2013-05-15 22:22:23 self.window = &lt;UIWindow: 0x1dd6ee80; frame = (0 0; 320 480); opaque = NO; autoresize = RM+BM; layer = &lt;UIWindowLayer: 0x1dd6ef80&gt;&gt;
  2013-05-15 22:22:23 self.window.subviews.count = 7
  2013-05-15 22:22:23 self.navigationController = &lt;UINavigationController: 0x1dd87190&gt;
  2013-05-15 22:22:23 self.navigationController.visibleViewController = &lt;MyViewController: 0x1dd7ffe0&gt;
  2013-05-15 22:22:23 self.navigationController.view = &lt;UILayoutContainerView: 0x1dd876e0; frame = (0 0; 320 480); autoresize = W+H; layer = &lt;CALayer: 0x1dd877a0&gt;&gt;
  2013-05-15 22:22:23 self.navigationController.viewControllers.count = 1
  2013-05-15 22:22:23 TestFlight: App Token is recognized
  2013-05-15 22:22:24 AppDelegate::applicationWillResignActive
  2013-05-15 22:22:24 TestFlight: End of Session
  </code></pre>
  
  <p>There are a few items in the log that concerned me at first, but I have observed them during normal operation so I no longer think that they are related.  Just in case, here they are:</p>
  
  <p>Immediately following ApplicationWillEnterForeground:</p>
  
  <pre><code>installd[54] &lt;Error&gt;: 0x2ff8d000 filter_attributes: Info.plist keys requested via MobileInstallationLookup/Browse in client Xcode (via mobile_installation_proxy) were not found in MobileInstallation's cache. Please file a bug requesting that these keys be added: &lt;CFBasicHash 0x1cd86080 [0x3c44d100]&gt;{type = mutable set, count = 18,
  entries =&gt;
      0 : &lt;CFString 0x3c4399f4 [0x3c44d100]&gt;{contents = "CFBundlePackageType"}
      1 : &lt;CFString 0x1cebf1e0 [0x3c44d100]&gt;{contents = "BuildMachineOSBuild"}
      2 : &lt;CFString 0x3c43aa44 [0x3c44d100]&gt;{contents = "CFBundleResourceSpecification"}
      3 : &lt;CFString 0x1ce90cf0 [0x3c44d100]&gt;{contents = "DTPlatformBuild"}
      4 : &lt;CFString 0x3c437794 [0x3c44d100]&gt;{contents = "DTCompiler"}
      5 : &lt;CFString 0x3c439564 [0x3c44d100]&gt;{contents = "CFBundleSignature"}
      6 : &lt;CFString 0x3c43a224 [0x3c44d100]&gt;{contents = "DTSDKName"}
      7 : &lt;CFString 0x1cebe5f0 [0x3c44d100]&gt;{contents = "NSBundleResolvedPath"}
      8 : &lt;CFString 0x3c436eb4 [0x3c44d100]&gt;{contents = "UISupportedInterfaceOrientations"}
      10 : &lt;CFString 0x3c43ee84 [0x3c44d100]&gt;{contents = "DTXcode"}
      13 : &lt;CFString 0x3c43eeb4 [0x3c44d100]&gt;{contents = "CFBundleInfoDictionaryVersion"}
      16 : &lt;CFString 0x3c43c304 [0x3c44d100]&gt;{contents = "CFBundleSupportedPlatforms"}
      17 : &lt;CFString 0x1ceabd10 [0x3c44d100]&gt;{contents = "DTXcodeBuild"}
      18 : &lt;CFString 0x1cebb610 [0x3c44d100]&gt;{contents = "UIStatusBarTintParameters"}
      19 : &lt;CFString 0x3c43ae54 [0x3c44d100]&gt;{contents = "DTPlatformVersion"}
      20 : &lt;CFString 0x3c43dbf4 [0x3c44d100]&gt;{contents = "DTPlatformName"}
      21 : &lt;CFString 0x3c43ec84 [0x3c44d100]&gt;{contents = "CFBundleDevelopmentRegion"}
      22 : &lt;CFString 0x1ceb9ae0 [0x3c44d100]&gt;{contents = "DTSDKBuild"}
  }
  </code></pre>
  
  <p>Shortly afterwards:</p>
  
  <pre><code>lockdownd[45] &lt;Notice&gt;: 2ff24000 special_case_get: MGCopyAnswer(kMGQReleaseType) returned NULL
  </code></pre>
  
  <h3><i>Removing TestFlight</i></h3>
  
  <p>This is a build distributed via TestFlight, which we have used successfully for several previous releases.  I have observed this behavior in a build deployed by Xcode as well, so that rules out TestFlight as a potential offender, at least in terms of the deployment.  Furthermore, I have removed the TestFlight calls from the code and tested the application.  After a few days, I observed the issue, so I know it's not related to TestFlight.</p>
  
  <h3><i>Verified that all NSURLConnections are performed asynchronously</i></h3>
  
  <h3><i>Verified that all updating of the UI is performed on the main thread</i></h3>
  
  <p>The only pieces of information I really have at this point is that somehow main and UIApplicationMain are no longer on the stack.  How can that happen?</p>
  
  <p>Also, this app uses SDWebImage and there are UIImageViews in the displayed view when this issue happens, so maybe there's an issue there, but I really doubt that such a highly used library would have such a glaring flaw.</p>