@properties - 内存管理

时间:2011-06-01 17:11:48

标签: iphone ios memory-management properties

这段代码对内存管理是否合适?

@property (nonatomic, retain) id object;
...
id anObject = [[Object alloc] init];
self.object = anObject;
id otherObject = [[Object alloc] init];
self.object = otherObject;

感谢您的回答, 基督教

1 个答案:

答案 0 :(得分:2)

没有。如属性描述符所示,它将在分配时保留对象。因此,当您分配它时,在将对象分配给self.object时,您的对象中将有两个保留。所以,你必须释放它:

@property (nonatomic, retain) id object;
...
id anObject = [[Object alloc] init];
self.object = anObject;
[anObject release];

id otherObject = [[Object alloc] init];
self.object = otherObject;
[otherObject release];
...
at dealloc:
self.object = nil;
祝你好运!