title: UIScrollView and real memory
body: <p>I watched WWDC 2010 104 session about implementing tiling on UIScrollView and I tried to implement it with some changes,but i have problem with real memory - I add UIImageView as subview to UIScrollView and i even tried to remove super view immediately after adding - and i have not my real memory cleaned.At all.
  May be there is a problem with autoreleasing?</p>
  
  <pre><code>- (void)tilePages 
  {
      [scrollView setContentSize:CGSizeMake(scrollView.bounds.size.width * [images count], scrollView.bounds.size.height)]; 
  
      CGRect visibleBounds = scrollView.bounds;
      int firstNeededPageIndex = floorf(CGRectGetMinX(visibleBounds) / CGRectGetWidth(visibleBounds));
      int lastNeededPageIndex  = floorf((CGRectGetMaxX(visibleBounds)-1) / CGRectGetWidth(visibleBounds));
      firstNeededPageIndex = MAX(firstNeededPageIndex, 0);
      lastNeededPageIndex  = MIN(lastNeededPageIndex, ([images count] - 1));
  
      for (ImageScrollView *page in visiblePages) {
          if (page.index &lt; firstNeededPageIndex || page.index &gt; lastNeededPageIndex) {
              [recycledPages addObject:page];
              [page removeFromSuperview];
          }
      }
  
      [visiblePages minusSet:recycledPages];
  
      for (int index = firstNeededPageIndex; index &lt;= lastNeededPageIndex; index++) {
          if (![self isDisplayingPageForIndex:index]) {
              ImageScrollView *page = [self dequeueRecycledPage];
              if (page == nil) {
                  page = [[[ImageScrollView alloc] init] autorelease];
  
                  [page setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
  
                  if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
                      [page setContentMode:UIViewContentModeScaleAspectFill];
                  } 
                  else {
                      [page setContentMode:UIViewContentModeScaleAspectFit];
                  }
              }
              [self configurePage:page forIndex:index];
  
              [scrollView addSubview:page];
  
              [visiblePages addObject:page];
          }
      }    
  }
  
  - (ImageScrollView *)dequeueRecycledPage
  {
      ImageScrollView *page = [recycledPages anyObject];
      if (page) {
          [[page retain] autorelease];
          [recycledPages removeObject:page];
      }
      return page;
  }
  
  - (BOOL)isDisplayingPageForIndex:(NSUInteger)index
  {
      BOOL foundPage = NO;
      for (ImageScrollView *page in visiblePages) {
          if (page.index == index) {
              foundPage = YES;
              break;
          }
      }
      return foundPage;
  }
  
  - (void)configurePage:(ImageScrollView *)page forIndex:(NSUInteger)index
  {
      page.index = index;
      page.frame = [self frameForPageAtIndex:index];
      [page displayImage:[self imageAtIndex:index]];
  }
  
  - (CGRect)frameForPagingScrollView 
  {
      CGRect frame = scrollView.bounds;
      return frame;
  }
  
  - (CGRect)frameForPageAtIndex:(NSUInteger)index {
      CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
  
      CGRect pageFrame = pagingScrollViewFrame;
      pageFrame.origin.x = (pagingScrollViewFrame.size.width * index);
      return pageFrame;
  }
  
  - (UIImage *)imageAtIndex:(NSUInteger)index {
      return [images objectAtIndex:index];   
  }
  
  - (void)viewDidUnload
  {
      [super viewDidUnload];
      self.scrollView = nil;
      self.textView = nil;
  }
  
  - (void)dealloc
  {
       [super dealloc];
       [scrollView release];
       [images release];
       [recycledPages release];
       [visiblePages release];
  }
  </code></pre>