开发者

Will releasing an NSMutableArray having allocated UIViews release the UIViews as well?

开发者 https://www.devze.com 2023-03-03 10:35 出处:网络
Ok so here\'s what I\'m doing. NSMutableArray *array = [[NSMutableArray alloc] init]; UIView *tempview = [[UIView alloc] initWithFrame:CGRectMake(15, 30, 320, 460)开发者_Go百科];

Ok so here's what I'm doing.

NSMutableArray *array = [[NSMutableArray alloc] init];  

UIView *tempview = [[UIView alloc] initWithFrame:CGRectMake(15, 30, 320, 460)开发者_Go百科];
[array addObject:tempView];

UIView *tempview2 = [[UIView alloc] initWithFrame:CGRectMake(15, 30, 320, 460)];
[array addObject:tempView2];

[array release];

Will the releasing of the array, release the two allocated UIViews as well?


If you copy, alloc, retain, or new something, you are responsible for sending it either release or autorelease.

You said [[UIView alloc] init...] so you must release the resulting object.


You are responsible for releasing the views since you created them. Here is how it goes:

You create the views with a retain count of 1. When they are added to the array, it will retain them (retain count = 2). When you release the array, it will release the views (retain count = 1). You still need to release them.

The correct code would be:

NSMutableArray *array = [[NSMutableArray alloc] init];  

UIView *tempview = [[UIView alloc] initWithFrame:CGRectMake(15, 30, 320, 460)];
[array addObject:tempView];
[tempview release];

UIView *tempview2 = [[UIView alloc] initWithFrame:CGRectMake(15, 30, 320, 460)];
[array addObject:tempView2];
[tempview2 release];

[array release];
0

精彩评论

暂无评论...
验证码 换一张
取 消