仪器泄漏、参考计数和自动释放
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Instruments 会为您添加保留和释放。这就是“RefCt”列向您显示的内容:运行总计。
确实如此;这是列表中的第一行。
自动释放不是立即-1;它会导致稍后释放,就是-1。
所以你有:
请注意,三个释放之一是由自动释放引起的。只有这样才会产生-1。
该对象需要另一个释放才能被释放。在那之前,它不会发生。
是的,一个具有足够的未完成的自动释放的对象有可能在它们到来之前杀死它,并在发生之前被保留,从而保持活动状态。我曾经在一个未保留的对象上看到过这种情况,但那是 Binding 使用的属性的值;绑定保留了该值,因此即使在我自动释放了它的最后所有权之后,它仍然保持活力。
Instruments adds up the retains and releases for you. That's what the “RefCt” column shows you: The running total.
And indeed it does; that's the first row in the list.
Autorelease isn't an immediate -1; it causes a release later, and that's the -1.
So you have:
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.