可能重复:
Is releasing memory of Objective-c 2.0 properties required?
例如:
@interface DataMode : NSObject {
NSString * name;
}
@property (nonatomic, retain) NSString * name;
@end
编译器会自动将[name release]
添加到-dealloc
吗?
- (void) dealloc
{
[name release]; // if we don't add it , will the compiler add "[name release]"???
[super release];
}
答案 0 :(得分:5)
这取决于您使用的内存管理方案:
使用垃圾收集,您不需要释放支持声明的属性的实例变量 - 垃圾收集器会自动执行该操作。实际上,即使您需要在重新分配时执行其他任务,也不会定义-dealloc
方法:垃圾收集器发送-finalize
而不是-dealloc
;
使用自动引用计数(ARC),您不会定义-dealloc
方法。 ARC将自动释放支持声明的属性的实例变量。如果需要,您可以定义-dealloc
方法来执行其他内务处理任务,但不会发送[super dealloc]
;
使用手动内存管理,您需要手动释放支持声明的属性的实例变量,然后发送[super dealloc]
。
答案 1 :(得分:0)
由于您正在添加或更确切地创建name
,因此您有责任将其发布。因此,您需要在[name release]
和dealloc
中使用ViewDidUnLoad
添加name = nil
。
ObjectiveC有垃圾收集,但在iOS垃圾收集部分已被剥离。所以分配,解除分配,保留等等你需要注意......