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];
}
}
}
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.
You never release the page you allocate at the bottom of your snippet (assuming you are not using ARC)