合成属性是否在dealloc中自动释放?

时间:2011-11-29 03:33:20

标签: objective-c memory-management properties

  

可能重复:
  How is release handled for @synthesized retain properties?

我看到有人提到某处(我不记得是谁或在哪里)合成的属性在dealloc中自动设置为nil,因此被释放。然而,仪器认为一切都只是内存泄漏,因此认为不同 哪一个是正确的?也许我错过了一个可以实现这一目标的功能? (这绝对不是我看到iOS 4.1出来的时候的弧度)

3 个答案:

答案 0 :(得分:1)

看看这个问题:How is release handled for @synthesized retain properties?

所以你必须自己释放它。它并不是为你自动完成的。

答案 1 :(得分:0)

不,他们不是。你必须在dealloc上发布它。

如果使用retain选项配置属性,则无论何时调用其setter,都会收到retain消息,因此需要将来release

答案 2 :(得分:0)

如果您使用的是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 
}

@end

相关问题