请参阅以下代码,它只是一个方法,我无法理解其中的内存管理问题,请告诉我,以下代码中发生了什么,以及为什么?
-(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];
}
这里是Potential leak of object allocated on line 112 and stored into close button
它在线上显示这样的消息
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
如何在此代码中进行适当的内存管理,并且可以详细指定
由于
答案 0 :(得分:1)
首先,您需要释放实例pickerView和closeButton。
根据规则,每当你分配一个对象时,它必须在作业完成后释放。在您的情况下,将实例添加到子视图后不是必需的,因此必须释放它们。否则它会显示您提到的泄漏。
如需更多理解,请参阅内存指南或快速查看http://www.raywenderlich.com/2657/memory-management-in-objective-c-tutorial