2008年2月27日 星期三

memory management in objective-c

before objective-c 2.0, there is no garbage collection
Hence, it is necessary to manage memory

every variable has a retain count. when its retain count becomes 0, it is released.

retain count在什麼時候會+1
(1)use "retain" on object: ex [dog retain]
(2) object created by "alloc", "copy", "new", "mutableCopy"
(3) when object is add to array

retain count在什麼時候會-1
(1)use "release" on object
(2)when object is removed from array

autorelease pool:
objects are added to autorelease pool when they are sent message "autorelease".
when the autorelease pool is deallocated, it sends message "release" to objects in the poll

沒有留言: