iPhone SDK:是Dealloc还是Release? [英] iPhone SDK: Dealloc vs. Release?
问题描述
我想知道release和dealloc之间有什么区别?阅读完内存管理规则(请参阅下文)之后,我在大多数时候都在考虑使用release.但是,我想知道如何处理属性.
I am wondering what the difference is between release and dealloc? After reading, the memory management rules (See below)., I am thinking most of the time I will be using release. However, I wanted to know what to do for properties.
@property(保留)....
@property(retain)....
我一直在使用dealloc,但是在阅读了这篇文章之后,我不确定那是正确的.
I have been using dealloc but after reading this article I am not sure that is correct.
如果使用名称以"alloc"或"new"开头或包含"copy"(例如alloc,newObject或mutableCopy)的方法创建对象,或者将其发送给对象,则您拥有该对象的所有权.保留消息.您有责任使用release或autorelease放弃您拥有的对象的所有权.其他任何时候,您都不能释放它.
You take ownership of an object if you create it using a method whose name begins with "alloc" or "new" or contains "copy" (for example, alloc, newObject, or mutableCopy), or if you send it a retain message. You are responsible for relinquishing ownership of objects you own using release or autorelease. Any other time you receive an object, you must not release it.
推荐答案
除了super
之外,请勿调用dealloc
.
您唯一要调用dealloc
的时间是在自定义继承对象的dealloc
方法中,它将是[super dealloc]
.
The only time you will be calling dealloc
is in the dealloc
method of a custom inherited object, and it will be [super dealloc]
.
当对象的保留计数减少到零时,将自动为您调用dealloc
,因此为了正确管理内存,您需要在适当的时候调用retain
和release
.
When the retain count of an object goes down to zero, dealloc
will automatically be called for you, so in order to manage memory properly, you need to call retain
and release
when appropriate.
如果您不清楚这点,或者想了解有关Cocoa中内存管理方式的详细信息,则应阅读
If this isn't clear to you or you'd like details about how memory is managed in Cocoa, you should read the Memory Management Programing Guide.
这篇关于iPhone SDK:是Dealloc还是Release?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!