【问题标题】:CSA warning: Incorrect decrement of the reference count of an object that is not owned at this point by the callerCSA 警告:调用者此时不拥有的对象的引用计数减少不正确
【发布时间】:2011-05-14 18:43:27
【问题描述】:

我刚刚在我的项目上运行了 clang 静态分析器,我收到以下警告:

Incorrect decrement of the reference count of an object that is not owned at this point by the caller

请你告诉我我的问题是什么。我通常能够很好地管理我的应用程序中使用的内存。

self.cupboardViewController = [[CupboardViewController alloc] initWithNibName:@"CupboardViewController" bundle:[NSBundle mainBundle]];
[self.window addSubview:self.cupboardViewController.view];

- (void)dealloc {
  [[self cupboardViewController] release];//where I am getting the warning.
  [super dealloc];
}

【问题讨论】:

  • 假设cupboardViewController 被标记为retain,无论如何你都会泄漏它,因为它会自动保留你在示例的第一行中创建的新对象。您应该在将其分配给属性之前自动释放该对象,à la self.cupboardViewController = [[[CupboardViewController alloc] init...] autorelease];
  • 我这样做了,但它导致应用程序在一段时间后崩溃。

标签: iphone objective-c cocoa-touch memory-management


【解决方案1】:

很确定您应该释放实例变量,而不是属性。

- (void)dealloc {
  [cupboardViewController release];
  [super dealloc];
}

【讨论】:

    【解决方案2】:

    如果您将 cupboardViewController 作为保留属性,则在上述代码中设置 self.cupboardViewController = 会创建保留计数 2。

    问题是它被过度保留了,所以当你在 dealloc 中释放它时,仍然有一个未完成的保留,因此它会泄漏。

    我使用的代码标准很简单:

    theProperty = [[NS* alloc] init]

    当我分配我的属性(创建单个保留)时,只需:

    [theProperty release];

    在我的dealloc 方法中。

    这样我的一致性在于我不引用属性,只引用 iVar 并回避这些问题,保留和释放过多或不足。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多