Potential leak of an object allocated on line 123

2019-08-01 12:46发布

The analyse tool gives a potential leak on this line of code.

for (int index = firstNeededPageIndex; index <= lastNeededPageIndex; index++) {

Can anybody help, because I can't figure it out.

Here is the rest of the code

    - (void)tilePages 
{


    // Calculate which pages are visible
    CGRect visibleBounds = pagingScrollView.bounds;
    int firstNeededPageIndex = floorf(CGRectGetMinX(visibleBounds) / CGRectGetWidth(visibleBounds));
    int lastNeededPageIndex  = floorf((CGRectGetMaxX(visibleBounds)-1) / CGRectGetWidth(visibleBounds));
    firstNeededPageIndex = MAX(firstNeededPageIndex, 0);
    lastNeededPageIndex  = MIN(lastNeededPageIndex, [self imageCount] - 1);

    // Recycle no-longer-visible pages 
    for (ImageScrollView *page in visiblePages) {
        if (page.index < firstNeededPageIndex || page.index > lastNeededPageIndex) {
            [recycledPages addObject:page];
            [page removeFromSuperview];
        }
    }
    [visiblePages minusSet:recycledPages];

    // add missing pages
    for (int index = firstNeededPageIndex; index <= lastNeededPageIndex; index++) {
        if (![self isDisplayingPageForIndex:index]) {
            ImageScrollView *page = [self dequeueRecycledPage];
            if (page == nil) {
                page = [[ImageScrollView alloc] init];
            }
            [self configurePage:page forIndex:index];
            [pagingScrollView addSubview:page];
            [visiblePages addObject:page];

        }
    }    

}

2条回答
Summer. ? 凉城
2楼-- · 2019-08-01 13:31

There will be a leak if a "page" added to recycledPages in // Recycle no-longer-visible pages section will never get reused in // add missing pages part.

I can't see declaration of recycledPages in your function, thus I assume they are tied to your view (or another object). Then, you need to free unused recycled pages when terminating or closing view (or whatever else condition you recognize).

If you are indeed doing it, then the analysis tool is probably not "bright enough" to recognize it.

查看更多
女痞
3楼-- · 2019-08-01 13:34

You never release the page you allocate at the bottom of your snippet (assuming you are not using ARC)

查看更多
登录 后发表回答