仪器泄漏、参考计数和自动释放

发布于 2024-12-17 01:57:49 字数 219 浏览 0 评论 0原文

Instruments 报告我 NSDate 变量泄漏。但是,如果我将保留和释放加起来,我认为应该能够通过自动释放池释放。可能我数错了,但我不想确定。看看 RefCt。 如果我 [Class alloc] 它的保留计数应该为 1,那么如果我自动释放该对象,它应该能够释放,或者不是吗?

在此处输入图像描述

Instruments is reporting me a leak of a NSDate variable. But If I add up the retains and releases it should be able to release I think, by the autorelease pool. Probably I'm counting wrong but I wan't to make sure. Take a look at the RefCt.
If I [Class alloc] it should come up with a retain count of 1, then if I autorelease that object, it should be able to free, or is it not?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

公布 2024-12-24 01:57:49

Instruments 会为您添加保留和释放。这就是“RefCt”列向您显示的内容:运行总计。

如果我[Class alloc],它的保留计数应该为 1,...

确实如此;这是列表中的第一行。

...那么如果我自动释放该对象,它应该能够释放,或者不是吗?

自动释放不是立即-1;它会导致稍后释放,就是-1。

所以你有:

  1. 分配:+1(=1)
  2. 自动释放:0现在;导致稍后释放(现在没有变化,所以仍然=1)
  3. 保留:+1(=2)
  4. 释放:-1(=1)
  5. 保留:+1(=2)
  6. 保留:+1(=3)
  7. 释放:-1 (=2)
  8. 释放:-1 (=1)

请注意,三个释放之一是由自动释放引起的。只有这样才会产生-1。

该对象需要另一个释放才能被释放。在那之前,它不会发生。

是的,一个具有足够的未完成的自动释放的对象有可能在它们到来之前杀死它,并在发生之前被保留,从而保持活动状态。我曾经在一个未保留的对象上看到过这种情况,但那是 Binding 使用的属性的值;绑定保留了该值,因此即使在我自动释放了它的最后所有权之后,它仍然保持活力。

Instruments adds up the retains and releases for you. That's what the “RefCt” column shows you: The running total.

If I [Class alloc] it should come up with a retain count of 1, …

And indeed it does; that's the first row in the list.

… then if I autorelease that object, it should be able to free, or is it not?

Autorelease isn't an immediate -1; it causes a release later, and that's the -1.

So you have:

  1. Allocation: +1 (=1)
  2. Autorelease: 0 for now; causes a Release later (no change now, so still =1)
  3. Retain: +1 (=2)
  4. Release: -1 (=1)
  5. Retain: +1 (=2)
  6. Retain: +1 (=3)
  7. Release: -1 (=2)
  8. Release: -1 (=1)

Note that one of the three Releases is the one caused by the Autorelease. Only then is -1 incurred.

The object needs another release in order to be deallocated. Until that happens, it won't.

And yes, it is possible for an object that has enough outstanding autoreleases to kill it when they come due to be retained before that happens and thereby be kept alive. I saw this happen once with an object that I was under-retaining, but that was the value of a property being used by a Binding; the Binding retained the value and so kept it alive even after I had autoreleased my own last ownership of it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文