Obj-C:retainCount> 1 在dealloc方法中?
如果在释放之前的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Apple 关于
-retainCount
的信息:From Apple regarding
-retainCount
:如果
myObject
通过方法(例如“method:”)传递给其他对象(例如“anObj”),如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 inanObj
can retainmyObject
if needed. Then it is perfectly reasonable that whendealloc
of the object containingmyObject
is called, the retain count ofmyObject
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 whenanObj
releases it.您想要做的不是打印出保留计数,而是针对您的代码运行 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.