有如下代码
headerViewArray = [[NSMutableArray alloc] init];
UIView* customView;
customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,40)];
[headerViewArray addObject:customView];
customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,40)];
[headerViewArray addObject:customView];
customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,40)];
[headerViewArray addObject:customView];
customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,40)];
[headerViewArray addObject:customView];
在dealloc函数中有:
[headerViewArray release];
问:上述customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,40)]; 是否产生了内存泄漏
是否需要调用
[customView release];
也就是变成如下:
headerViewArray = [[NSMutableArray alloc] init];
UIView* customView;
customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,40)];
[headerViewArray addObject:customView];
[customView release];
customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,40)];
[headerViewArray addObject:customView];
[customView release];
customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,40)];
[headerViewArray addObject:customView];
[customView release];
customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,40)];
[headerViewArray addObject:customView];
[customView release];
才不会产生内存泄漏
headerViewArray = [[NSMutableArray alloc] init];
UIView* customView;
customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,40)];
[headerViewArray addObject:customView];
customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,40)];
[headerViewArray addObject:customView];
customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,40)];
[headerViewArray addObject:customView];
customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,40)];
[headerViewArray addObject:customView];
在dealloc函数中有:
[headerViewArray release];
问:上述customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,40)]; 是否产生了内存泄漏
是否需要调用
[customView release];
也就是变成如下:
headerViewArray = [[NSMutableArray alloc] init];
UIView* customView;
customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,40)];
[headerViewArray addObject:customView];
[customView release];
customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,40)];
[headerViewArray addObject:customView];
[customView release];
customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,40)];
[headerViewArray addObject:customView];
[customView release];
customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,40)];
[headerViewArray addObject:customView];
[customView release];
才不会产生内存泄漏
[customView release];
customView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,300,40)] autorelease];也可以:
customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,40)];
[headerViewArray addObject:customView];
[customView release];