【问题标题】:Got warning "Incorrect decrement of the reference count of an object that is not owned at this point by the caller"收到警告“调用者此时不拥有的对象的引用计数减少错误”
【发布时间】:2011-10-12 13:29:21
【问题描述】:

当“分析”时,收到警告“调用者此时不拥有的对象的引用计数减少错误”。 (xCode 4.1+)

我可以理解最好从它的适当类中“杀死”一个对象,但是有没有办法在没有警告的情况下从外部执行它?

谢谢!

NSLog(@"--[%s:%d]",__PRETTY_FUNCTION__, __LINE__);
myAppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
   if (appDelegate.referencesPopup != nil) {
        [appDelegate.referencesPopup release]; // warning
        appDelegate.referencesPopup = nil;  
   }      
}

【问题讨论】:

    标签: objective-c xcode4


    【解决方案1】:

    只需将属性设置为nil,这里不需要释放它,正如分析器告诉你的那样。如果您没有保留它,请不要释放它。

    此外,您不应在点符号 getter 返回的内容上调用 release。此处已对此进行了广泛讨论:Why shouldn't I use the getter to release a property in objective-c?

    【讨论】:

      【解决方案2】:

      很可能,您获取存储在referencesPopup 中的对象的方式,不是您自己分配或保留它。在这种情况下,您不负责发布它。您可以简单地将 ivar 设置为 nil。

      【讨论】:

      • 嗨。谢谢,但我还是很困惑。如果我在 appDelegate.referencePopUp=Nil 之后重新分配对象,我会造成内存泄漏,不是吗? (referencePupUp =nil 并不意味着retaincount 减少)
      • referencesPopup 最初是如何分配的?你是分配一个对象还是从其他地方获取一个对象?如果是后者,那么将referencesPopup 设置为 nil 是无害的;如果您没有分配(或复制)一个对象,那么您不负责释放它。
      • appDelegate.referencesPopup = [[References alloc] init];
      • 好的,所以您正在分配它,并且因为您正在使用 setter 方法访问 referencesPopup,所以您将获得一个 retain。因此,在上面的代码中,您不需要自己调用 release,因为通过再次使用 setter 方法将 referencesPopup 设置为 nil,您会为您完成 release。所以这就是为什么你调用 release 是多余的。
      • appDelegate.referencesPopup = [[References alloc] init] 您好,根据您的 cmets,这可能存在内存泄漏,不是吗? [[References alloc] init] = retaincout++ and appDelegate.referencesPopup = retaincount++ (user the setter =implicit retain) 所以,正确的行应该是 (?) appDelegate.referencesPopup = [[References alloc] init]autorelease];还在开始理解,ARC 来了... :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多