Release、Dealloc 和 Self 引用 [英] Release, Dealloc, and the Self reference

查看:18
本文介绍了Release、Dealloc 和 Self 引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想我已经弄清楚了所有这些问题.然后突然间我收到一个我无法弄清楚的错误(崩溃).然后在进行研究以修复崩溃之后,我发现我认为我对这些关键领域的了解有些错误.

So I thought I had all these questions all figured out. Then all of a sudden I get an error (a crash) I can't figure out. Then after doing research to remedy the crash, I notice everything that I thought I knew about these critical areas are somewhat wrong.

以下是我将要提出的 8 个问题,希望有人能回答——这些问题的答案将极大地帮助我重新理解我的理解.提前致谢!

Below are 8 questions I am just going to shoot out there in hopes of somebody answering - the answers to these will greatly help me get my understanding back on track. Thanks ahead of time!

Q1) 如果引用为 nil,可以对对象调用 Release 吗?这应该是无害的,对吧?

Q1) Is it okay to call Release on an object if that reference is nil? This should be harmless, right?

Q2) 如果该引用已被释放并且引用计数为 0,则可以在该对象上调用 Release 吗?

Q2) Is it okay to call Release on an object if that reference has been released and as a reference count of 0?

Q3) 是否有必要在释放后将引用设置为 nil?如果不将其设置为 nil 会发生什么?

Q3) Is it necessary to set a reference to nil AFTER releasing it? What happens if you dont set it to nil?

Q4) nil 和 NULL 之间真的有区别吗,还是只是一种语义上的东西,可以帮助读者/开发人员通过一眼就知道对象类型?

Q4) Is there really a difference between nil and NULL, or is it just a semantic thing to help the reader/developer know the object type just by glancing at it?

Q5) 使用属性需要使用 'Self' 指针吗?

Q5) Using properties REQUIRE the use of the 'Self' pointer?

Q6) 使用实例变量需要不使用Self"指针?

Q6) Using instance variables requires that the 'Self' pointer is NOT used?

Q7) 我什么时候想要使用实例变量而不是它的属性?我认为值类型数据成员没问题,因为它们不涉及释放和保留.

Q7) When would I ever want to use an instance variable instead of its property? I'd imagine value type data members are okay since their is not releasing and retaining involved.

Q8) 是否有必要从 dealloc 函数中调用对象的 dealloc?在许多示例中,我看到调用 Release,但没有调用 Dealloc - 这样的教程不正确吗?

Q8) Is it necessary to call an object's dealloc from within the dealloc function? In many examples I have seen Release being called, but not Dealloc - are such tutorials incorrect?

推荐答案

A1) [nil release] 很好(不会做任何事情)

A1) [nil release] is fine (won't do anything)

A2) 不可以.不要在对象被释放后触摸它们.释放后应将它们设置为 nil.

A2) No. Don't touch objects after they've been deallocated. They should be set to nil after they are released.

A3) 没有必要将释放的指针设置为 nil,但是您会得到悬空指针(即,您无法判断对象是否有效).将属性设置为 nil 通常用于释放底层的 ivar,因此如果不这样做可能会导致内存泄漏

A3) It's not necessary to set a released pointer to nil, but you get dangling pointers (i.e., you can't tell if an object is valid or not). Setting a property to nil is often used to release the underlying ivar, so failing to do this can cause a memory leak

A4) nil 和 NULL 都为零,因此在技术上是相同的.

A4) nil and NULL are both zero, so are technically the same.

A5) 是的,你必须使用 self.someProperty 作为属性,就像你会使用 [self someProperty] 如果它只是一个方法

A5) Yes, you must use self.someProperty for properties, just as you would use [self someProperty] if it was just a method

A6) self 本质上是一个结构体,因此您可以像这样访问 ivars:self->someIvar.不过没必要.

A6) self is essentially a struct, so you can access ivars like so: self->someIvar. There is no need to, though.

A7) 当您出于任何原因不想运行 setter/getter 方法时.当 setter 不允许 nil 值时,我偶尔会使用它,并且我需要释放变量

A7) When you don't want to run the setter/getter methods for whatever reason. I use it ocassionally when the setter doesn't allow nil values, and I need to release the variable

A8) 当 release 被调用正确的次数时会自动调用 dealloc.永远不要直接调用 dealloc(除了 [super dealloc])

A8) dealloc is called automatically when release is called the correct amount of times. You should never call dealloc directly (except for [super dealloc])

这篇关于Release、Dealloc 和 Self 引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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