【发布时间】:2012-02-15 00:01:49
【问题描述】:
在初始化方法中,我有以下代码
- (id)init {
self = [super init];
if (self) {
UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
tempButton.frame = CGRectMake(0,0,300,44);
// some custom code...
self.myButton = tempButton;
}
return self;
}
其中myButton 是保留属性。
我知道,就内存管理规则而言,此方法等于其他方法:
- (id)init {
self = [super init];
if (self) {
UIButton *tempButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,300,44)];
// some custom code...
self.myButton = tempButton;
[tempButton release];
}
return self;
}
但在这种情况下,我需要使用第一个“版本”,因为 buttonType 属性是只读的,并且在按钮初始化后我无法更改它。
由于我发现自己在整个应用程序的多个方法中使用“非初始化发布”版本,并且对于几个对象(其中大多数是 NSString),我的问题是:在这种情况下不计算分配给保留对象的属性,何时释放tempButton 对象?也许在方法/if语句的末尾?还是第一个“版本”会导致内存使用量增加,因为对象不是立即释放而是在一定时间后释放?
【问题讨论】:
标签: ios memory-management retain