Question

I create UIViews based on xib files programmaticaly and add them to a scrollView. This works so far. If the amount changes I recreate the grid of UIViews (in my case so called theme thumbs). I remove the old ones before I recreate the new ones. The problem is the memory is never released and for every new creation memory usage piles up. Even if I remove one single thumb the memory isn´t released.

Instruments doesn´t show leaks. Dealloc in my ViewController is never called.

I use ARC and I know that I have to avoid retained references. But clearly I have used them and don´t understand where. It would be great if someone could give me a hint where I did it as I have read a lot of posts here but still don´t understand it. NSZombiesEnabled is off.

My code:

-(void)createGrid
{
    for (UIView *subview in self.scrollView.subviews) {
        if ([subview tag] < 10000 && ![subview isKindOfClass:[UIImageView class]]) {
            [subview removeFromSuperview];
        }
    }
    int col = 0;
    int row = 0;
    [self setupFetchedResultsController];
    for (int i = 0; i<[[self.fetchedResultsController fetchedObjects] count]; i++)
    {
        col = i % THEME_COLUMNS;
        row = i / THEME_COLUMNS;
        ThemeThumbVC *themeThumb = [[ThemeThumbVC alloc] init];
        [self.scrollView addSubview:themeThumb.view];

        // configurate thumb
        themeThumb.managedObjectContext = self.managedObjectContext;
        themeThumb.fetchedResultsController = self.fetchedResultsController;
        themeThumb.theme = [[self.fetchedResultsController fetchedObjects] objectAtIndex:i];
        [themeThumb.themeThumbImage setImage:[UIImage imageWithContentsOfFile:[[[self.fetchedResultsController fetchedObjects] objectAtIndex:i] iconImageURL]]];
        [themeThumb setTag:i];
        [themeThumb.view setTag:i];
        [themeThumb.view setFrame:CGRectMake(col*kThemeGritXOffset+(col*1), row*kThemeGritYOffset, kThemeGritXOffset+1, kThemeGritYOffset)];
        [self addChildViewController:(UIViewController*) themeThumb];
    }
    [self.scrollView setContentSize:CGSizeMake(320, row*kThemeGritYOffset+kThemeGritYOffset)];
}
Was it helpful?

Solution

If a thumb view is encapsulated by a child view controller, then wouldn't you need to remove the child view controller from the parent view controller at some point?

If the child view controller is not being removed from the parent view controller, then my best guess is that the child view controller is retaining its thumb view.

Have you thought about using a UICollectionViewController to create a grid of thumb views?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top