Iphone @属性-内存管理
关于内存管理,这段代码可以吗Iphone @属性-内存管理,iphone,ios,memory-management,properties,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; 谢谢你的回答, Christian否。如属性描述符所示,它将在分配对象时保留该对象。因此,在分配时,在将对象分配给self
@property (nonatomic, retain) id object;
...
id anObject = [[Object alloc] init];
self.object = anObject;
id otherObject = [[Object alloc] init];
self.object = otherObject;
谢谢你的回答,
Christian否。如属性描述符所示,它将在分配对象时保留该对象。因此,在分配时,在将对象分配给
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;
祝你好运 我喜欢在可用时使用方便的方法,而在不可用时,我总是使用[[[Class alloc]init]autorelease]。看起来比有一条额外的线来释放看起来很奇怪的对象更漂亮。但我想这是一个品味的问题,这也是一个创建自动释放对象的问题。假设OP的代码处于一个紧密的循环中:显式释放比构建大量自动释放的内存要好得多。