Iphone 如何使用产品发布对象>;在XCode中分析?
请看下面的代码,这只是一个方法,我无法理解其中的内存管理问题,请告诉我,下面的代码中发生了什么,为什么Iphone 如何使用产品发布对象>;在XCode中分析?,iphone,memory-management,Iphone,Memory Management,请看下面的代码,这只是一个方法,我无法理解其中的内存管理问题,请告诉我,下面的代码中发生了什么,为什么 -(IBAction)selectMarina { NSLog(@"Select Marina"); actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil can
-(IBAction)selectMarina {
NSLog(@"Select Marina");
actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
//[pickerView release];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
//[closeButton release];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
[pickerView selectRow:0 inComponent:0 animated:NO];
}
这里说的是在第112行分配并存储到关闭按钮中的对象的潜在泄漏
它在网上显示这样的信息
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
如何在这段代码中进行适当的内存管理,您能详细说明吗
谢谢首先,您需要释放instance pickerView和Close按钮 根据规则,每当你分配一个对象,一旦它完成任务,就必须释放它。在您的情况下,将这两个实例添加到子视图后,它们都不是必需的,因此必须释放它们。否则,它将显示您提到的泄漏
要了解更多信息,请参阅《内存指南》或快速查看,请单击“为什么我要发布pickerView?它是全局的,也不是本地的,那么您如何说要发布它?我没有抓住这一点。”。若实例是全局声明的,那个么必须在dealloc方法中发布。但我用两个方法初始化它,那个么?没问题。您可以多次初始化ivar。记住一件事(alloc的数量=发布的数量)。最后,ivar的保留计数必须为零。