NSMutableArray memory management

后端 未结 5 1036
情歌与酒
情歌与酒 2020-12-09 08:48
NSMutableArray *a1 = [[NSMutableArray alloc] init];
NSMutableArray *a2 = [NSMutableArray array];

TempObj *obj = [[TempObj alloc] init]; //assume this line is repeat         


        
5条回答
  •  自闭症患者
    2020-12-09 09:08

    It's easier to think of Objective-C memory management as ownership rather than in terms of retain-release. When you add the objects to the array, the array is now a co-owner of the object and is responsible for properly managing the memory. When the owner (whatever object contains the code you posted) calls release on the objects, it's giving up ownership and now the array is the sole owner.

    Apple has a good primer on how ownership works in Cocoa (including how you know when you are responsible for calling release on an object): Memory Management Programming Guide For Cocoa. It's a must-read if you want to use Cocoa.

提交回复
热议问题