何时“释放”设置属性/实例变量后?
我们声明了一个属性:
@property (retain) MyClass *myProperty;
有什么区别
MyClass *aux = [[MyClass alloc] init];
myProperty = aux;
[aux release];
Apple 示例代码中的这个与此
myProperty = [[MyClass alloc] init];
:编辑:
发布的原始代码应该是这个:
MyClass *aux = [[MyClass alloc] init];
self.myProperty = aux;
[aux release];
这是我的一个错误,但由于许多答案都涵盖了我的主题留下了原来的代码。
We have a property declared:
@property (retain) MyClass *myProperty;
What is the difference between this one from Apple example Code:
MyClass *aux = [[MyClass alloc] init];
myProperty = aux;
[aux release];
and this one:
myProperty = [[MyClass alloc] init];
Edited:
The original code posted should have been this one:
MyClass *aux = [[MyClass alloc] init];
self.myProperty = aux;
[aux release];
It was an error of mine, but since many answers have covered the topic I have leave the original code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这通常是将属性设置为您创建的新值的正确方法。
这在 init 方法中是可以接受的。
您发布的代码是错误的。
下面的代码有一个微妙的错误。
但是,如果访问器以一种有趣的方式编写,并且 getter 不返回传递给 setter 的相同对象,您可能会遇到麻烦。例如,它可能返回某种代理对象。那么你就不会释放你分配的同一个对象。
This is generally the right way to set a property to a new value you've created.
This is acceptable in the init method.
The code you posted is wrong.
The following code is subtly wrong.
However you might get into trouble if the accessors are written in a funny way, and the getter doesn't return the same object you passed into the setter. Perhaps it returns some sort of proxy object, for example. Then you would not be releasing the same object you alloc'ed.
首先,为了稍微澄清一下声明属性的含义,我将对其进行一些解释。
当你声明一个属性时,你实际上是在声明两个方法,即该特定类属性的 getter 和 setter。当您将属性声明为
retain
时,您实际上是在说,当您通过 setter 方法设置该属性时,它将被保留。这基本上意味着它的保留计数将增加。为了使用声明的属性设置类属性,您可以使用点语法,例如
self.myProperty
或 setter 方法,例如-(void)setMyProperty:(MyClass*)newMyClass
,因此,在您的代码中,即使您声明了一个属性,您也没有使用它,因为您没有使用上述任何方法。
现在,
MyClass
对象,现在该对象的保留计数为 1。myProperty = [[MyClass alloc] init];
MyClass
对象(它的保留计数为 1)并为其分配类属性myProperty
。因此,总而言之,在第一种情况下,您在内存中创建并对象,然后处置它,而在第二种情况下,您正在创建它,但它永远不会被处置。
Firs of all, just to clarify a little bit what declaring a property means I will explain it a little.
When you declare a property, you are actually declaring two methods, a getter and a setter for that particular class attribute. When you declare a property as
retain
you are actually saying that when you set that property through the setter method, it will be retained. That basically means that its retain count will be incremented.In order to set the class attribute using the property declared, you can either use dot syntax, e.g.
self.myProperty
or the setter method, e.g.-(void)setMyProperty:(MyClass*)newMyClass
,So, in your code, even though you are declaring a property, you are not making use of it because you are not using any of the methods stated above.
Now,
MyClass
object, now that object has retain count of 1.myProperty = [[MyClass alloc] init];
MyClass
object (it has retain count of 1) and assign it your class attributemyProperty
.So, to sum up, in the first case you create and object in memory and then you dispose it, whereas in the second one you are creating it but it never gets disposed.
第一行泄漏是因为它使用属性设置器来分配新对象,并且该属性具有保留的内存模型。因此,除了分配中的分配之外,您还可以从属性的设置器中获得保留。
但是,第二行不会泄漏,因为它不使用属性的 setter,而是使用其后面的私有变量。一般来说,除了 init 之外,您希望在任何地方使用 setter。
由于属性设置器会增加保留计数(对于保留/复制内存模型),因此在属性分配中看到自动释放的情况并不罕见,例如:
如果您想真正了解它,则覆盖的设置器可能看起来像这样:
The first line leaks because it is using the property setter to assign a new object and the property has a memory model of retain. So, in addition to the alloc in the assignment, you get a retain from the property's setter.
However, the second line will not leak as it is not using the property's setter but the private variable behind it. Generally speaking, you want to use the setter everywhere except in init.
Because property setters increase the retain count (for retain / copy memory models), it's not uncommon to see an autorelease in property assignments like:
If you want to really wrap your head around it, an overridden setter might look something like this:
在第一种情况下,myProperty 的保留计数为 0。
在第二种情况下,myProperty 的保留计数为 1。
如果您在第一种情况下使用 self.myProperty = aux ,则 aux 和 myProperty 的保留计数将为 1。
In first case myProperty have retainCount 0.
In second case myProperty have retainCount 1.
If you will use self.myProperty = aux in first case then retainCount of aux and myProperty will be 1.