【问题标题】:Memory Leak - Incorrect decrement of the reference count of an object that is not owned at this point by the caller内存泄漏 - 调用者此时不拥有的对象的引用计数不正确递减
【发布时间】:2013-10-30 01:35:10
【问题描述】:

使用“分析”,在 dealloc 中我得到:调用者此时不拥有的对象的引用计数的不正确递减

- (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];
}

}

如何解决?谢谢。

【问题讨论】:

  • [dataSource imageAtIndex:page] 给你一个借来的参考。如果你不拥有它,你就不能释放它。
  • 兄弟,你是对的。非常感谢。@DavidK.Hess

标签: memory memory-leaks ios7


【解决方案1】:

查看Basic Memory Management Rules

您的 [dataSource imageAtIndex:page] 为您提供了对视图的引用——但不是所有权。您只能“释放”您拥有的对象。您可以通过以“alloc”、“new”、“copy”或“mutableCopy”开头的方法或显式调用“retain”来获得对象的所有权。

【讨论】:

  • 当我删除“[view release];”时分析没有问题。非常感谢。
猜你喜欢
  • 2011-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多