0
votes

With an 'Analyze', in the dealloc I get: Incorrect decrement of the reference count of an object that is not owned at this point by the caller

- (void)loadScrollViewWithPage:(int)page {
    int kNumberOfPages = [dataSource numberOfPages];

if (page < 0) return;
if (page >= kNumberOfPages) return;

// replace the placeholder if necessary
UIImageView *view = [imageViews objectAtIndex:page];
if ((NSNull *)view == [NSNull null]) {
    view = [dataSource imageAtIndex:page];
    [imageViews replaceObjectAtIndex:page withObject:view];
    [view release]; //<--here 
}
// add the controller's view to the scroll view
if (nil == view.superview) {
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    view.frame = frame;
    [scrollView addSubview:view];
}

}

How to solve? Thanks.

1
[dataSource imageAtIndex:page] is giving you a borrowed reference. If you don't own it, you can't release it. - David K. Hess
Bro,you're right.Thanks a [email protected] - Gener Kill

1 Answers

1
votes

Check out Basic Memory Management Rules.

Your [dataSource imageAtIndex:page] gives you a reference to view – but not ownership. You can only "release" objects you own. You get ownership of an object via methods that begin with “alloc”, “new”, “copy”, or “mutableCopy” or an explicit call to "retain".