简体   繁体   中英

Memory Management, ARC - what to nil?

Background -

I am using automatic reference counting on a project. The root view is a Table View (Master / Detail setup) showing a list of "slide shows". Click on a table cell and you are taken to the detail view which consists of a Scroll view with views (viewController.view) in it (this is the "slide show"). Each slide show has a front cover and back cover (same view controller formatted differently) that sandwich an variable number of pages. Here is the code to load the slide show:

- (void)loadScrollView
{    
// The front and back cover are set in Interface Builder because they
// are reused for every slide show, just labels are changed.
[self.scrollView addSubview:self.frontCoverViewController.view];
[self.frontCoverViewController setCoverTitle:_data.name creationDate:_data.creationDate isFrontCover:YES];
[self.pagesArray addObject:self.frontCoverViewController];

for (int i = 0; i < [self getTotalNumberOfComps]; i++) 
{
    PageViewController *pageView = [[PageViewController alloc] init];
    pageView.data = [_compsArray objectAtIndex:i];

    [_scrollView addSubview:pageView.view];
    pageView.data.imgView = pageView.imageView;
    pageView.slideShowViewController = self;
    [_pagesArray addObject:pageView];
}

[self.scrollView addSubview:self.backCoverViewController.view];
[self.backCoverViewController setCoverTitle:_data.name creationDate:_data.creationDate isFrontCover:NO];
[self.pagesArray addObject:self.backCoverViewController];

[self.scrollView bringSubviewToFront:_frontCoverViewController.view];
[self setCurrentPage:0];
}

Problem -

So Im trying to reuse this slide show view controller so I need to nil and recreate the pages in the middle because each slide show has a different number of slides. Note a slide [PageViewController] is just a view with an ImageView in it. It has more functionality so we need the controller however the main display of the VC is the ImageView. I have created the following method to "empty" the slide show before running loadScrollView again with new data. Here is the empty method:

- (void)saflyEmptyScrollView
{
for (int i = 0; i < [self.pagesArray count]; i++) 
{
    if (i == 0 && i == ([self.pagesArray count]-1)) {
        CoverViewController *cover = (CoverViewController*)[self.pagesArray objectAtIndex:i];
        [cover.view removeFromSuperview]; 
    } else {
        PageViewController *page = (PageViewController*)[self.pagesArray objectAtIndex:i];
        [page.view removeFromSuperview];
        page = nil;
    }
}
self.pagesArray = nil;
self.pagesArray = [[NSMutableArray alloc] init];
} 

Big Question -

My main question is do I need to set the ImageView of each of these pages to nil? Or does setting the page itself to nil also free up the memory used by the ImageView/Labels/etc that are used in that view controller?

I tried adding self.imageView = nil; to the PageViewController's viewDidUnload and viewWillUnload methods (one at a time not in both) and I realized that setting page = nil does not call the pages Unload methods. Am I freeing up memory correctly.

I've read a lot of articles but Im still not sure if Im managing memory in the best way possible. Thanks so much for the help!

Generally, you shouldn't have to set things to nil. And in this specific case, the setting things to nil is doing nothing.

The line page = nil; is redundant, because the variable page goes out of scope immediately afterwards anyway. ARC knows this and doesn't need you to set it to nil.

And self.pagesArray = nil; is redundant because you follow it with self.pagesArray = [[NSMutableArray alloc] init]; . The second line on its own will suffice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM