我需要在 dealloc 中释放吗?
在我正在学习的 iPhone 开发书中,他们使用 Interface Builder 来利用 IBOutlet 实例。一个例子是 UIButton
。因此,他们在结构中添加了这样的内容:
IBOutlet UIButton *whateverButton;
然后他们为 .h 中的每个属性添加一个 @property
,并在 .m 中添加一个 @synthesize
。
然后,他们在 .m 的 dealloc
中包含一个 release
。两个问题:
- 是否需要发布?不是所有属性都已经自动处理了吗?
- 我如何检查引用计数以了解发生了什么,以进行调试......?
In the book I'm studying from for iPhone dev, they utilize IBOutlet
instances using the Interface Builder. An example would be a UIButton
. So they add a thing in the struct like this:
IBOutlet UIButton *whateverButton;
Then they add a @property
for each of these in the .h, and a @synthesize
in the .m.
Then they include a release
in the dealloc
of the .m. Two questions:
- Is the release necessary? Aren't all properties already handled automatically?
- How can I check the ref count to see what's happening, for debug purposes...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果保留财产,则需要释放。当你声明一个@property和@synthesize它时,你得到的只是访问器,dealloc中没有特殊的自动行为。
另外,IBOutlet 并没有什么神奇之处——它只是 Interface Builder 的一个标记,用于查看您希望在 IB 中出现哪些属性。它只是一个空宏,按住 Cmd 键单击 IBOutlet 关键字即可查看其定义:
IBAction 也是如此,它扩展为
void
。当我需要调试内存管理时,我通常只是在 dealloc 方法中设置一个断点或在那里记录一个字符串。在调用周围记录对象的
retainCount
也很有帮助,这些调用可能会对其产生可疑的影响。了解 @synthesize 指令如何创建访问器也可能有所帮助。当您声明保留的@property并要求编译器对它们进行@synthesize时,您会得到类似这样的信息:
这不完全是这样,但它已经足够接近了。现在应该更清楚为什么应该在 dealloc 中释放了。
If the property is retained, the release is necessary. When you declare a
@property
and@synthesize
it, all you get is the accessors, there is no special automatic behaviour in dealloc.Also, there is nothing magical about IBOutlet – it’s just a marker for Interface Builder to see which properties you would like to appear in IB. It’s simply an empty macro, Cmd-click the IBOutlet keyword to see its definition:
Same thing goes for IBAction which expands to
void
.When I need to debug memory management, I usually simply set up a breakpoint in the dealloc method or log a string there. It is also helpful to log the
retainCount
of an object around the calls that might do something fishy with it.It might also help to see how the
@synthesize
directive creates the accessors. When you declare a retained@property
and ask the compiler to@synthesize
them, you get something like this:This isn’t exactly the thing, but it’s close enough. Now it should be more clear why you should release in dealloc.
属性不是“自动处理的”。最接近事实的是合成访问器正确处理其内存管理职责。但这只是那些访问器。属性只是在类上声明可访问“事物”的一种方式。除此之外,他们没有得到太多特殊待遇。它不会开启某种垃圾收集。所以是的,释放是必要的。
如果您想检查正在运行的应用程序是否存在泄漏或未释放的内存,则应该使用 Instruments 等调试工具。我不会直接查看引用计数,因为它几乎毫无用处——无法保证引用计数在任何时候都是您所期望的,这并不一定表明存在问题。
您应该阅读 Apple 的 Cocoa 内存管理规则。一旦你吸收了这一点,事情就非常简单了。我不一定建议先阅读其他指南,因为细微的错误陈述可能会导致您走上错误的道路(例如,将为您发布属性的想法可能来自于听到有人错误地陈述了它们的工作原理)。
Properties are not "handled automatically." The closest that comes to being true is that synthesized accessors handle their memory management responsibilities properly. But that is just those accessors. Properties are just a way of declaring accessible "things" on your class. They don't get much special treatment beyond that. It doesn't turn on some sort of garbage collection. So yes, release is necessary.
And you should use the debugging tools like Instruments if you want to inspect a running app for leaks or memory that doesn't get released. I would not look at the ref count directly, because it's almost dangerously useless — there's no guarantee that the ref count will be what you expect at any point, and that doesn't necessarily indicate a problem.
You should read Apple's memory management rules for Cocoa. It's pretty simple once you've absorbed that. I wouldn't necessarily recommend reading other guides first, because subtle misstatements can lead you down the wrong path (for instance, the idea that properties will be released for you probably came from hearing somebody misstate how they work).
这取决于属性如何实现。如果它是自动实现的(
@synthesize
'd),则该属性将在 setter 中保留其值,并在设置为其他值时释放它。如果您刚刚接触 Obj-C 和 Cocoa,您应该阅读有关内存管理的约定。我在我的博客上发布了帖子关于它们,其他地方也有大量资源。您可以检查 NSObject keepCount 属性。有关信息
It depends on how the property is implemented. If it is auto-implemented (
@synthesize
'd), the property will retain its value in the setter and release it if set to another value. If you just got into Obj-C and Cocoa, you should read about the conventions for memory management. I have put up a post on my blog about them, there are plenty of resources elsewhere too.You can check the NSObject retainCount property. Information on that is here. For advanced debugging purposes, there is the NSZombieEnabled environment flag that will cause all release message to not decrement the reference count but log an error when an object that would have normally been released is accessed.