【问题标题】:Are synthesized properties automatically released in dealloc? [duplicate]合成的属性会在dealloc中自动释放吗? [复制]
【发布时间】:2012-01-08 11:34:38
【问题描述】:
【问题讨论】:
标签:
objective-c
memory-management
properties
【解决方案2】:
不,他们不是。您必须在 dealloc 上释放它。
如果属性配置了retain 选项,则无论何时调用其设置器,它都会收到retain 消息,因此需要未来的release。
【解决方案3】:
如果您使用 ARC 并且您的 ivars 是objective-c 对象,那么它们会为您释放:)
示例:
@interface Car : NSObject {
}
@property (nonatomic, retain) NSString *modelName;
@property (nonatomic) char *moreInfo;
@end
@implementation Car
@synthesize modeName;
@synthesize moreInfo;
- (id)init{
self = [super init];
if (self) {
moreInfo = (char *)malloc(128*sizeof(char)); // malloc'ed,
}
return self;
}
- (void)dealloc{
free(moreInfo); // malloc'ed vars are not automatically released
//[modelName release]; // Since we are using ARC this is provided by the compiler
//[super dealloc]; // Since we are using ARC this is provided by the compiler
}
@结束