【发布时间】:2011-05-15 18:35:28
【问题描述】:
通常当我创建一个对象并将其分配给一个实例变量时,我分配一个临时对象,调用 iVar 设置器来保留该对象,然后释放该临时对象。但是,我今天早上查看 init 并注意到,如果我只是直接分配 iVar,它会由 alloc 保留,同时在调用 setter 或执行 dealloc 时也会正确释放。我只是好奇我是否理解正确?
@property(nonatomic, retain) CLLocationManager *locationManager;
.
@synthesize locationManager;
// VERSION 001
- (id)init {
self = [super init];
if(self) {
CLLocationManager *tempManager = [[CLLocationManager alloc] init];
[self setLocationManager:tempManager];
[tempManager release];
}
return self;
}
// VERSION 002
- (id)init {
self = [super init];
if(self) {
locationManager = [[CLLocationManager alloc] init];
}
return self;
}
- (void)dealloc {
[locationManager release];
[super dealloc];
}
【问题讨论】:
标签: iphone objective-c memory-management