Objective C Array and Object Release

后端 未结 3 779
不知归路
不知归路 2020-12-20 17:57

I have a newbie question regarding when to release the elements of a NSArray. See following pseudo code:

NSMutalbeArray *2DArray = [[NSMutableArray alloc] in         


        
3条回答
  •  天命终不由人
    2020-12-20 18:40

    When an object is created, it has a retain count of 1. Whenever a object is added to an array, its retain count is increased (in this case to 2). After adding to the array, your code release its hold of the object, dropping its retain count by 1 (to 1 in this case). Then when you release the array, it calls release on everything in it dropping their retain counts by 1 (to 0 in this case). When retain count hits 0 the object is deallocated.

    Your code looks correct from a memory management stand point.

提交回复
热议问题