jin*_*ini 0 iphone memory-management objective-c ios
我正在做一个教程,作者给出了一个例子,但我很好奇为什么他没有在函数结束时释放childController.有什么想法吗?
-(void)tableView:(UITableView *) tableView
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
if (childController==nil) {
childController = [[DisclosureDetailController alloc] initWithNibName:@"DisclosureDetailController" bundle:nil];
}
childController.title=@"Disclosure Button Pressed"; //why this line?????
NSUInteger row = [indexPath row];
NSString *selectedMovie = [list objectAtIndex:row];
NSString *detailMessage = [[NSString alloc]initWithFormat:@"you pressed disclosure button for %@",selectedMovie];
childController.message = detailMessage;
childController.title = selectedMovie;
[detailMessage release];
[self.navigationController pushViewController:childController animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
似乎childController是这个类中的一个字段,因此(希望)在dealloc部分中释放它.
他/她实际上只创建了一次.
编辑:
在对childController进行真正的nil检查后,它会获得1的保留计数并分配给该字段.如果未提前释放childController,则在整个类的实例的生命周期内只执行一次.