iOS内存管理跟进。 dealloc vs nil? [英] iOS Memory Management followup. dealloc vs nil?

查看:128
本文介绍了iOS内存管理跟进。 dealloc vs nil?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于这个帖子:
iPhone - dealloc - Release vs. nil

1) [foo release];
2) self.bar = nil;

解释如下:


  1. 释放对象,通过实例变量 bar 访问它。实例变量将成为悬空指针。这是dealloc中的首选方法。

  1. Is releasing the object, accessing it through the instance variable bar. The instance variable will become a dangling pointer. This is the preferred method in dealloc.

将nil分配给self上的属性栏,实际上会释放当前保留的属性。如果你有一个属性的自定义setter,那么这样做,应该清理不仅仅是支持属性的实例变量。

Is assigning nil to a property bar on self, that will in practice release whatever the property is currently retaining. Do this if you have a custom setter for the property, that is supposed to cleanup more than just the instance variable backing the property.

有人可以澄清#1的解释吗?通过实例变量 bar 访问?

Could someone please clarify the explanation for #1? Accessed through the instance variable bar ?

例如,我在对象标题中设置了私有var,如下所示:

E.g I have set a private var in my object header as so:

SomeObject *oPointer;

我没有在头文件中使用带有此指针的属性设置器,它确实 NOT 在实例化对象时合成。

I am not using a property setter with this pointer in the header file and it does NOT get synthesized when instantiate the object.

在我的代码中,给定某些条件,我稍后必须分配并将此指针指定给它的对象。

In my code, given certain conditions, I later have to allocate and assign this pointer to it's object.

obj = [[SomeObject alloc] initWith....];

所以现在可以通过实例变量 obj 访问它。我有一个UIButton配置为重置此对象它的附加方法deallocs它。我这样做是通过:

So this is now accessed through the instance variable obj. I have a UIButton which is configured to RESET this object it's attached method deallocs it. I do this via:

[obj release];
obj = nil;

在解释问题的所有内容之后,我为什么还拥有来声明obj = nil? [obj release] 调用似乎也会杀死指针。我当时认为 [obj release] 会释放它所指向的内存并将 obj 设置为nil all in one shot但它似乎也会杀死指针,因为我的app在 [obj release] 之后尝试引用 obj 时崩溃;

After all that explaining the question is, why do I also have to declare the obj = nil? The [obj release] call seems to also kill the pointer. I was thinking that [obj release] would deallocate the memory it's pointing to and also set obj to nil all in one shot but it seems it also kills the pointer because my app crashes when it tries to reference obj after [obj release];

这个问题有意义吗?解释只是 [obj release] 进行 ALL 清理,包括查杀指针,我需要注意这一点吗?

Does this question make sense? Is the explanation simply that [obj release] does ALL cleanup, including killing the pointer and I need to be aware of that?

如果我为SomeObject指针设置了retain属性,指针在发布后仍会保留吗?

If I set a retain property for the SomeObject pointer would the pointer still be retained after release?

提前致谢!

推荐答案

调用发布会减少参考指望obj。如果引用计数变为0,则将其取消分配。指针obj仍指向相同的内存位置,但访问它可能会导致程序崩溃。将obj设置为nil并非绝对必要,但强化了obj不再有效的观点。它可以在调试时使用,或者稍后在程序中有用,如果你想要重新创建obj并使用if(obj!= nil)检查它是否已经创建。

Calling release will decrease the reference count on obj. If the reference count goes to 0, then it will be deallocated. The pointer obj still points to the same memory location, but accessing it will likely cause the program to crash. Setting obj to nil is not strictly necessary, but reinforces the idea that obj is no longer valid. It can be useful when debugging, or later in the program if you want to optionally re-create obj and use a "if (obj != nil)" check to see if it's been created already.

如果你把它作为属性设置为retain,那么当它被称为self.obj = someObj时,Objective C会将一个加到someObj的引用计数中。你永远不应该在通过alloc-init创建的东西上调用自己,因为alloc-init已经将引用计数设置为1.如果保留了该对象,那么当它被释放时,引用计数只会返回到1,它会成为内存泄漏。

If you set retain on it as a property, then when it's called as "self.obj = someObj", Objective C will add one to someObj's reference count. You should never call retain yourself on something that is created via alloc-init, since alloc-init already sets the reference count to 1. If you retained that object, then when it was released, the reference count would only go back to 1, and it would become a memory leak.

这篇关于iOS内存管理跟进。 dealloc vs nil?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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