title: Termination due to memory pressure with known cause, unknown solution
body: <p>My app is terminating due to memory pressure at a certain point in the app's use, and I have isolated the problem down to one chunk of code that is causing the problem. </p>
  
  <p>I'll copy the code chunk below, but first I'll describe what it's doing. </p>
  
  <p><strong>The Description</strong>:</p>
  
  <p>I have a for loop that is iterating through a list of videos. For each video, the for loop increases the size of the containing scrollview, draws a label and a button (both pertaining to the video), and asynchronously grabs a thumbnail of the video and places it below the button. </p>
  
  <p><strong>The Problem</strong>:</p>
  
  <p>The grabbing the thumbnail part is the problem. I don't think that the fact that this is done asynchronously is the problem, as I have tried it synchronously and the termination still occurs. When I comment out the code that grabs the thumbnail (the entire asynchronous part in the code below) the app doesn't crash. </p>
  
  <p><strong>The Code</strong>:</p>
  
  <p>NB: I'm using comments to replace code in some cases for brevity. </p>
  
  <pre><code>for (int i = [_videoURLs count]-1; i &gt;= 0 ; i--)
      {
          //increase the scrollview size
  
          //get background image:
          dispatch_async(screenshotQueue, ^{
  
              AVURLAsset *as = [[AVURLAsset alloc] initWithURL:currentURL options:nil];
              AVAssetImageGenerator *ima = [[AVAssetImageGenerator alloc] initWithAsset:as];
              ima.appliesPreferredTrackTransform = YES;
              NSError *err = NULL;
              CMTime time = CMTimeMake(1, 24);
              CGImageRef imgRef = [ima copyCGImageAtTime:time actualTime:NULL error:&amp;err];
              UIImage *thumbnail = [[UIImage alloc] initWithCGImage:imgRef];
  
              dispatch_async(dispatch_get_main_queue(), ^{
  
                  UIImageView *backgroundImage = [[UIImageView alloc]initWithFrame:buttonFrame];
                  backgroundImage.image = thumbnail;
                  backgroundImage.layer.opacity = 0;
                  [self.videoScrollView addSubview:backgroundImage];
  
                  [self.videoScrollView sendSubviewToBack:backgroundImage];
  
                  [UIView animateWithDuration:0.5 delay:0.0 options: UIViewAnimationOptionCurveEaseInOut
                   animations:^{
                       backgroundImage.layer.opacity = 1;
                   }
                   completion:^(BOOL finished){}];
  
              });
  
          });
  
          //add title to the screen caps
  
          //draw the button
  
      }
  </code></pre>
  
  <p>I'm on Xcode 5, testing on a new device with iOS 7. I'm self taught, so I'm sure I've picked up my fair share of bad habits, but I'm hoping I've missed something obvious that someone with a little more experience will pick up on. </p>
  
  <p>I've tried a fair amount of googling and searching on stack overflow, and have tried looking at diagnostics, logs and memory profiles to isolate the problem further, to no avail. </p>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p><strong>Edit</strong>:</p>
  
  <p>How to reproduce the error: </p>
  
  <ol>
  <li>Go to video list</li>
  <li>Leave video list</li>
  <li>Repeat 1+2 4 or 5 times causes the crash</li>
  </ol>
  
  <p>Step 2. simply removes the entire scrollView from its superview and then redraws it. I understand it's not necessary to redraw the video list every time you go back to it, but I'm doing this for a certain reason and I don't believe this is the source of the problem (I could be wrong).  </p>
  
  <p><strong>Edit 2</strong>:</p>
  
  <p>Here's an image of the memory profile:
  <img src="https://i.stack.imgur.com/5Rd3E.png" alt="enter image description here"></p>