为什么iPhone示例代码使用这么多中间变量? [英] Why does iPhone sample code use so many intermediate variables?
问题描述
我目前正在研究Apress的"iPhone 3开发初期".他们在示例应用程序中使用的标准类似于以下代码:
I'm currently working through Apress's "Beginning iPhone 3 Development". A standard they use in their example applications is like the following code:
- (void)viewDidLoad {
BlueViewController *blueController = [[BlueViewController alloc]
initWithNibName:@"BlueView" bundle:nil];
self.blueViewController = blueController;
[self.view insertSubview:blueController.view atIndex:0];
[blueController release];
}
8.14.11更新(附加信息)
blueViewController声明如下:
8.14.11 UPDATE (additional information)
blueViewController is declared as follows:
@property (retain, nonatomic) BlueViewController *blueViewController;
每当执行alloc
时,便将其放入某个临时变量(此处为blueController
)中,然后对其进行分配,然后将其释放.这个临时变量对我来说似乎是多余的.
我将代码简化如下:
Whenever they perform an alloc
they put it in some temp variable (here blueController
) then they assign it, then they release it. This temp variable seems superfluous to me.
I simplified the code as follows:
- (void)viewDidLoad {
self.blueViewController = [[BlueViewController alloc]
initWithNibName:@"BlueView" bundle:nil];
[self.view insertSubview:blueViewController.view atIndex:0];
}
- (void)dealloc {
[blueViewController release];
[super dealloc];
}
我修改后的代码在iPhone模拟器中的运行方式完全相同.
现在,我知道一条规则,即如果您分配某些内容,则需要释放它.我将在dealloc
方法中进行介绍.但是直接在ViewDidLoad
(调用alloc
的函数)中具有发布是否有好处?还是在您的dealloc
方法中使用这样的release
同样可以吗?
感谢您的帮助,
-j
My modified code ran just the same in the iPhone simulator.
Now, I know the rule that if you alloc something you need to release it. And I'm covering that in my dealloc
method. But is there some advantage to having a release directly in the ViewDidLoad
(the function where the alloc
was called)? Or is it equally ok to have a release
in your dealloc
method like this?
Thanks for any help,
-j
推荐答案
假定blueViewController
是retain
属性,则临时变量不是多余的.您的简化正在造成内存泄漏.来自第二个片段的该语句泄漏:
Assuming blueViewController
is a retain
property, the temporary variable is not superfluous. Your simplification is creating a memory leak. This statement from the second snippet leaks:
self.blueViewController = [[BlueViewController alloc]
initWithNibName:@"BlueView" bundle:nil];
就所有权而言,您拥有alloc-init返回的对象,然后属性访问器再次声明该对象的所有权,从而导致该对象过度保留.
In terms of ownership, you own the object returned by alloc-init and then the property accessor claims ownership of the object again, resulting in the object being over-retained.
使用临时变量可以解决此问题.另一种选择是使用autorelease
:
Using a temporary variable solves this problem. Another option is to use autorelease
:
self.blueViewController = [[[BlueViewController alloc]
initWithNibName:@"BlueView" bundle:nil] autorelease];
请注意,在此语句之后,您实际上拥有该对象,并且必须在dealloc中释放它.
Note that after this statement you effectively own the object and you must release it in dealloc.
您没有提到如何声明属性blueViewController
.无论如何,无论setter的语义是什么(retain
,copy
,assign
),该陈述都是错误的.我已经解释了最可能的情况:retain
.让我们看一下其他两个可能性(不考虑它们是否完全有意义):
You did not mention how the property blueViewController
is declared. Anyway, whatever the semantics of the setter are (retain
, copy
, assign
), that statement is wrong. I already explained the most likely scenario: retain
. Let's have a look at the other two possibilites (without considering if they make sense at all):
-
如果
blueViewController
恰好是copy
属性,则该语句也会泄漏.属性访问器复制原始对象,现在该属性持有指向副本的指针,您丢失了对原始对象的跟踪,立即泄漏了该对象.
If
blueViewController
happened to be acopy
property, the statement would leak too. The property accessor copies the original object and now the property holds a pointer to the copy and you lost track of the original object, immediately leaking it.
最不可能的情况是blueViewController
是assign
属性,因为这很可能是错误的,并且您确实想要retain
.但是,无论如何,assign
属性适用于您您不拥有的对象,例如代表,您不应该释放他们.您正在为对象拥有,因此您泄漏了该对象或错误地释放了assign属性.
The least likely scenario is that blueViewController
is an assign
property because this is most likely wrong and you really want retain
. But, anyway, the assign
properties are for objects you do not own, e.g. delegates, and you are not supposed to release them. You are assigning an object you own to it, so either you leak it or you incorrectly release the assign property.
这篇关于为什么iPhone示例代码使用这么多中间变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!