在 dealloc 中使用 self.property = nil 有什么问题吗? [英] Is there any problem using self.property = nil in dealloc?

查看:19
本文介绍了在 dealloc 中使用 self.property = nil 有什么问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道声明的属性会生成访问器方法,这在某种程度上只是语法糖.

我发现很多人在他们的 dealloc 方法中使用了 self.property = nil.

1) 在 Apple 的内存管理文档中,第 23 页它说:

<块引用>

唯一不应该使用访问器方法来设置实例变量的地方是在 init 方法和 dealloc 中.

为什么不应该?

2) 在苹果的 Objective-C 2.0, p74

<块引用>

声明的属性从根本上取代了访问器方法声明;当您合成一个属性时,编译器只会创建任何不存在的访问器方法.与 dealloc 方法没有直接交互 - 不会为您自动释放属性.但是,声明的属性确实提供了一种有用的方法来交叉检查 dealloc 方法的实现:您可以在头文件中查找所有属性声明,并确保对象属性未标记 assign 被释放,标记为 assign 的那些不会被释放.

注意:通常在 dealloc 方法中,您应该直接release 对象实例变量(而不是调用 set accessor 并传递 nil 作为参数),如本例所示:

- (void)dealloc { [财产释放];[超级dealloc];}

<块引用>

如果您使用的是现代运行时并合成实例变量,则无法直接访问实例变量,因此必须调用访问器方法:

- (void)dealloc { [self setProperty:nil];[超级dealloc];}

注释是什么意思?

我发现 [property release];[self setProperty:nil]; 都有效.

解决方案

设置属性可能会导致向其他观察该属性的对象发送通知.这反过来可能会导致这些对象尝试对您的对象做进一步的处理.如果您正在解除分配,这可能不是您想要发生的.所以一般来说,直接release相关的实例变量更安全.

请注意,此类问题只会在某些情况下出现,因此通常完全有可能在 dealloc 中使用 self.property=nil 编写代码以及用于所有内容锻炼得很好.这不是最佳实践.

在 Objective-C 现代运行时"中,可以在不指定 ivar 的情况下声明属性.运行时将合成存储以与合成的访问器一起使用.在这种情况下,您不能直接释放 ivar,因为就您的代码而言,没有一个.所以你别无选择,只能走 self.property=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 your dealloc method: you can look for all the property declarations in your header file and make sure that object properties not marked assign are released, and those marked assign are not released.

Note: Typically in a dealloc method you should release object instance variables directly (rather than invoking a set accessor and passing nil 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.

解决方案

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 in dealloc 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.

这篇关于在 dealloc 中使用 self.property = nil 有什么问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆