Obj-C:retainCount> 1 在dealloc方法中?

发布于 2024-09-06 10:38:37 字数 489 浏览 12 评论 0原文

如果在释放之前的 dealloc 方法中对象的保留(引用)计数大于 1,这是否意味着会出现内存泄漏?

我正在调试代码以发现另一个问题,但后来遇到了这个微妙的问题。我的对象之一在 dealloc 方法中的保留计数为 3。该对象是带有retain的属性,仅在类内部调用。现在我想在释放之前,dealloc 方法中的所有对象的保留计数应该为 1,对吗?

下面是自定义类中的示例 dealloc 方法:

- (void)dealloc {
    // Prints: "myObject retaincount: 3"
    NSLog(@"myObject retaincount: %d", [myObject retainCount]);

    // myObject retain count will be 2 after this call
    [myObject release];

    [super dealloc];
}

这是正常的吗?

If a retain (reference) count of an object is greater than 1 in a dealloc method right before releasing does this mean there will be a memory leak?

I was debugging my code to find another issue but then ran into this subtle one. One of my object's retain counts was 3 in the dealloc method. This object is a property with retain and is only called within the class. Now I imagine that the retain count should be 1 for all objects in the dealloc method before releasing right?

Here's a sample dealloc method in a custom class:

- (void)dealloc {
    // Prints: "myObject retaincount: 3"
    NSLog(@"myObject retaincount: %d", [myObject retainCount]);

    // myObject retain count will be 2 after this call
    [myObject release];

    [super dealloc];
}

Is this normal?

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

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

发布评论

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

评论(3

我三岁 2024-09-13 10:38:37

Apple 关于 -retainCount 的信息:

重要提示:此方法通常用于
调试内存没有价值
管理问题。因为任何数
的框架对象可能保留了
一个对象以保存引用
到它,同时
自动释放池可能持有任何
延迟发布的数量
对象,你不太可能
可以从中得到有用的信息
方法。

From Apple regarding -retainCount:

Important: This method is typically of
no value in debugging memory
management issues. Because any number
of framework objects may have retained
an object in order to hold references
to it, while at the same time
autorelease pools may be holding any
number of deferred releases on an
object, it is very unlikely that you
can get useful information from this
method.

许仙没带伞 2024-09-13 10:38:37

如果 myObject 通过方法(例如“method:”)传递给其他对象(例如“anObj”),如

 [anObj method:myObject];

anObj 可以保留 myObject 如果需要的话。那么当调用包含myObject的对象的dealloc时,myObject的retain count大于1是完全合理的。

你的代码是仍然可以:包含对象的责任是在使用完毕后释放所有权。 [myObject release] 之后,myObject 将不会被释放。相反,当 anObj 释放它时,它将被释放。

If myObject is passed to some other object (say 'anObj') via a method (say 'method:') as in

 [anObj method:myObject];

anObj can retain myObject if needed. Then it is perfectly reasonable that when dealloc of the object containing myObject is called, the retain count of myObject is more than 1.

Your code is still OK: the responsibility of the containing object is to release ownership when it's done with it. After [myObject release], myObject won't be dealloc'ed. Instead, it will be dealloc'ed when anObj releases it.

森林散布 2024-09-13 10:38:37

您想要做的不是打印出保留计数,而是针对您的代码运行 ObjectAlloc Instrument,并打开引用计数(单击该仪器的 (i) 并重新启动记录您的应用程序的仪器)。

然后,您可以转到对象的特定实例,并获取它的完整保留/释放历史记录 - 这可以帮助确定是否有某些东西保留了不应该保留的对象。

另一个快速检查是设置一个断点或将日志放入 myObject 类的 dealloc 中,以便您可以直接查看对象是否在您期望的时候被释放 - 可能它仍然有更大的保留计数在那里,但是一旦自动释放池被清空,它就会消失。

What you want to do instead of printing out the retain count, is to run the ObjectAlloc Instrument against your code, with reference counting turned on (click on the (i) of the instrument and re-start the instrument recording your app).

Then you can go to a specific instance of an object, and get the full retain/release history for it - that can help figure out if something is retaining the object that should not be.

Another quick check would be to set a breakpoint or put a log in dealloc of whatever class myObject is, so that you can see directly if the object is getting deallocated when you expect it to - it could be that it's still got a larger retain count there, but once the autorelease pool is emptied it will go away.

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