问题描述
dealloc方法中的语句顺序是否重要? [super dealloc]
是否需要位于方法的顶部?这有关系吗?
Does the order of statements in the dealloc method matter? Does the [super dealloc]
need to be at the top of the method? Does it matter?
viewDidLoad中
。应该 [super viewDidLoad]
位于方法的顶部?
Also in e.g. viewDidLoad
. Should [super viewDidLoad]
be at the top of the method?
推荐答案
绝对很重要。
您所做的取决于您使用的是自动参考计数(ARC)还是手动参考计数。
What you do depends on whether you're using Automatic Reference Counting (ARC) or manual reference counting.
手动释放 - 保留(MRR)是所有Mac OS版本的默认存储器管理X,以及处理内存直到Xcode 4.2的唯一方法。
Manual Release-Retain (MRR) is default memory management for all versions of Mac OS X, and the only way to handle memory until Xcode 4.2.
使用MRR, [super dealloc]
应该是在您的方法的 结束 。
With MRR, [super dealloc]
should be at the end of your method.
因此您的代码应如下所示:
So your code should look like this:
- (void)dealloc
{
[member release];
[super dealloc];
}
super dealloc实际上释放了内存。考虑一下。如果你之后访问一个实例变量,如下所示:
The super dealloc actually frees the memory. Think about that. If you access an instance variable after that, like this:
[super dealloc];
[member release];
...这意味着实例变量可能无效。在对super dealloc的调用和对成员释放的调用之间,理论上存储成员指针的字节可能已被改为其他东西!
...it means that the instance variable is potentially invalid. Between the call to super dealloc and the call to member release, theoretically the bytes that stores the member pointer could have been changed to something else!
正如Apple在:
您可以通过处置对象所拥有的任何资源来执行此操作,以及调用 [super dealloc]
。它处理它所拥有的任何对象,并调用它的超级。依此类推,直到最终根对象将实例本身使用的内存标记为空闲。当 [super dealloc]
返回时,您的实例已被释放。 (当然,它中的指针可能有效,但这是你不应该依赖的实现细节。)
You do this by disposing of any resources your object holds, and calling [super dealloc]
. Which disposes any objects it holds, and calls its super. And so on, and so on, until eventually the root object marks the memory used by the instance itself as free. By the time [super dealloc]
returns, your instance has been freed. (Of course, the pointers in it are probably valid, but that's an implementation detail you shouldn't rely on.)
一般来说,构建(或加载)让超级工作先做。撕毁时,先做好工作。
Generally, when constructing (or loading) let the super do the work first. When tearing things down, do your work first.
参见:
- ,NSObject类参考,Mac OS X开发人员库
- ,对象所有权和处置,内存管理编程指南,Mac OS X开发人员库
- dealloc, NSObject Class Reference, Mac OS X Developer Library
- Deallocating an Object, Object Ownership and Disposal, Memory Management Programming Guide, Mac OS X Developer Library
自动引用计数(ARC)是Xcode 4.2中引入的新内存管理方式。使用ARC,编译器在编译应用程序时会添加内存管理代码。它有一些你想要在使用它之前想要阅读更多的皱纹(大多数情况下,与旧的OS版本的兼容性有限)。
Automatic Reference Counting (ARC) is the new way of doing memory management introduced in Xcode 4.2. With ARC, the compiler adds memory management code when compiling your application. It has some wrinkles you'll want to read more about before using it (mostly, limited compatibility with older OS versions).
使用ARC,你没有(和不能)根本调用 [super dealloc]
。相反,当你的 dealloc
结束时,会调用 [super dealloc]
。
With ARC, you don't (and can't) call [super dealloc]
at all. Instead, [super dealloc]
is called when your dealloc
finishes.
参见:
- LLVM dealloc
- LLVM Automatic Reference Counting dealloc
这篇关于正确[super dealloc]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!