2008年6月24日 星期二

memory management in objective-c

retain:
increase retain count

release:
decrease retain count

array:
when an object is added to the array, the object's retain count is increased
when the array is deallocated,  the object( in array) 's retain count is decreased 

dealloc:
when an object's retain count becomes 0, its dealloc method is called

autorelease:
the object is added to autorelease pool when it is sent message autorelease
when the pool is drained, it sends message release to all objects in the pool

objects created by alloc, new, copy or mutableCopy have a retain count of 1 & are not in the autorelease pool 

if the object is get from other method, it is usually in the autorelease pool. If you do not want it to be deallocated when the pool drain, you must retain it

memory control for setter:
retain , then release
ex:
-(void)setName:(NSString*)name
{
      [name retain];
      [myName release];
      myName= name;
}

沒有留言: