在dealloc中使用self.property = nil有什么问题吗?
我知道声明的属性会生成访问器方法,这在某种程度上只是语法糖。
我发现很多人在他们的dealloc方法中使用self.property = nil。
1) 在Apple的内存管理文档中,p23 它说:
唯一不应该使用访问器方法来设置实例变量的地方是 init 方法和 dealloc。
为什么不应该?
2) 在苹果的Objective-C 2.0中,p74
声明的属性从根本上取代了访问器方法声明;当您综合属性时,编译器仅创建任何不存在的访问器方法。与
dealloc
方法没有直接交互——不会自动为您释放属性。但是,声明的属性确实提供了一种有用的方法来交叉检查dealloc
方法的实现:您可以在头文件中查找所有属性声明,并确保对象属性不存在标记分配
的已释放,标记分配
的未释放。注意:通常在
dealloc
方法中,您应该直接release
对象实例变量(而不是调用 set 访问器并传递nil
作为参数),如本例所示:
- (void)dealloc { [property release]; [super dealloc]; }
但是,如果您使用现代运行时并合成实例变量,则无法直接访问实例变量,因此必须调用访问器方法:
- (void)dealloc { [self setProperty:nil]; [super dealloc]; }
注释是什么意思?
我发现 [property release];
和 [self setProperty:nil];
都有效。
I know declared property generates accessor method which is someway just syntax sugar.
I found quite a lot people use self.property = nil
in their dealloc
method.
1) In Apple's Memory Management document, p23
It says:
The only places you shouldn’t use accessor methods to set an instance variable are in init methods and dealloc.
why shouldn't?
2) In apple's Objective-C 2.0, p74
Declared properties fundamentally take the place of accessor method declarations; when you synthesize a property, the compiler only creates any absent accessor methods. There is no direct interaction with the
dealloc
method—properties are not automatically released for you. Declared properties do, however, provide a useful way to cross-check the implementation of yourdealloc
method: you can look for all the property declarations in your header file and make sure that object properties not markedassign
are released, and those markedassign
are not released.Note: Typically in a
dealloc
method you shouldrelease
object instance variables directly (rather than invoking a set accessor and passingnil
as the parameter), as illustrated in this example:
- (void)dealloc { [property release]; [super dealloc]; }
If you are using the modern runtime and synthesizing the instance variable, however, you cannot access the instance variable directly, so you must invoke the accessor method:
- (void)dealloc { [self setProperty:nil]; [super dealloc]; }
What does the note mean?
I found [property release];
and [self setProperty:nil];
both work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置属性可能会导致通知发送到观察该属性的其他对象。这反过来可能会导致这些对象尝试对您的对象做进一步的事情。如果您正处于释放过程中,这可能不是您希望发生的情况。所以一般来说直接
释放
相关的实例变量会更安全。请注意,这类问题只会在某些情况下出现,因此通常完全可以在
dealloc
中使用self.property=nil
编写代码,并且一切都能解决美好的。这只是不是最佳实践。在 Objective-C“现代运行时”中,可以在不指定 ivar 的情况下声明属性。运行时将合成存储以与合成的访问器一起使用。在这种情况下,您无法直接释放 ivar,因为就您的代码而言,不存在 ivar。所以你别无选择,只能走 self.property=nil 路线。
Setting a property can lead to notifications being sent to other objects that are observing that property. That could in turn lead to those objects attempting to do something further with your object. If you are in the middle of deallocating, this is probably not what you want to happen. So in general it is safer to
release
the relevant instance variable directly.Note that this sort of problem will only arise in certain cases, so it is often perfectly possible to write code using
self.property=nil
indealloc
and for everything to work out fine. It's just not best practice.In the Objective-C "modern runtime", it is possible to declare properties without ever specifying the ivar. The runtime will synthesise the storage to go along with the synthesised accessors. In this case you cannot release the ivar directly because as far as your code is concerned there isn't one. So you have no choice but to go the
self.property=nil
route.