Objective-C 设置 nil 和释放 nil 的区别
我了解到,在 dealloc
中,您执行 [object release];
但在 viewDidUnload
(在 UIViewController 子类中)中,您执行 self .object = nil
。 真正的区别是什么,因为 self.object = nil
(我们假设 object 是一个 (nonatomic, keep)
属性)保留 nil
(什么都不做),然后释放旧值,然后引用计数为 0,对吗?
I've learned that in dealloc
you do [object release];
but in viewDidUnload
(in a UIViewController subclass) you do self.object = nil
. What is really the difference because self.object = nil
(we're assuming object is a (nonatomic, retain)
property) retains nil
(which does nothing) and then releases the old value and then the reference count is 0 right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您执行[对象释放],并且想要访问该对象,则应用程序只会崩溃。
如果您执行 object = nil,并且想要访问该对象,则不会执行任何操作。
原因是在[对象释放]中,您试图释放该对象。 所以它没有任何指针(没有记忆)。在
object = nil,
中,您尝试为对象分配空指针。 所以如果你尝试访问该对象,什么也不会发生。一般我们把代码写成[
object release]; object = nil;
因为如果你意外访问该对象,应用程序不会崩溃。If you do [object release], and want to access the object the app simply crash.
If you do object = nil, and want to access the object nothing will perform.
The reason is in the [object release], you are attempted to free the object. so its dont have any pointer (no memoty).In the
object = nil,
you are attempted to assign the object with null pointer. so if u trying to access the object nothing will happen.Generally we wrote the code as [
object release]; object = nil;
because if u access the object unexpectedly, the app wont crashed.如果你只是释放一个对象,那么它将成为释放对象。
如果您尝试对释放的对象执行任何类型的操作,那么您的应用程序就会崩溃。 为了避免此类事故,始终首选“释放对象后将其分配为 nil”。 因为我们都知道对 nil 执行的任何操作都不会被执行:)
If you just release an object, then it will become freed object.
And if you try to perform any sort of operation on freed object then your app crashes. To avoid such accidents, it is always preferred "assign your object to nil after releasing it". Because we all know any operations performed on nil will not be executed :)
如果您在没有
[object release]
的情况下执行object = nil
,则可能会导致内存泄漏。 如果您随后执行[object release]
而没有object = nil
,则对象将成为悬空指针,如@Jim建议的那样。self.object = nil
是setter函数调用的糖。If you do
object = nil
without[object release]
, that might causes memory leaking. If you do[object release]
withoutobject = nil
afterwards, object becomes dangling pointer as @Jim suggested.self.object = nil
is a sugar for setter function call.self.object = nil 调用您的 setter,它将释放旧值,将成员设置为 nil,并可能执行其他操作(这是一个方法,因此它可以执行任何事物)。 其中的“任何事情”部分都有潜在的危险; 请参阅此问题,例如。
[object release]
释放旧值,但将成员保留为现在悬空的指针,这是产生错误的好方法。 在dealloc中这并不重要,因为指针本身也即将消失,但在任何其他情况下释放成员而不将其设置为nil都是一个非常糟糕的主意/代码>。(顺便说一句,你永远不应该假设释放一个对象时它的引用计数为 0。它释放了你的引用,但其他对象可能仍然有对它的引用。)
self.object = nil
calls your setter, which will release the old value, set the member tonil
, and possibly do other things (it's a method, so it could do anything). The "anything" part of that is potentially dangerous; see this question, for example.[object release]
releases the old value, but leaves the member as a now-dangling pointer, which is a good recipe for bugs. Indealloc
it doesn't really matter, since the pointer itself is about to go away too, but in any other case it's a very bad idea to release a member without setting it tonil
.(As a sidenote, you should never assume that releasing an object gives it a reference count of 0. It releases your reference, but other objects may still have references to it.)