2011-11-16 22 views
0

請參閱下面的代碼,它只是一個方法,我無法理解其中的內存管理問題,請告訴我,下面的代碼中發生了什麼,以及爲什麼?如何使用產品>在XCode中分析來釋放對象?

-(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]]; 

我怎樣才能做到在這段代碼正確的內存管理,並且可以指定它在細節

謝謝

回答

1

首先,您需要釋放實例pickerView和closeButton。

按照規則,只要你分配一個對象,它必須在它的任務完成後被釋放。在你的情況下,將實例添加到子視圖後不需要,因此他們必須發佈。否則,它會顯示你提到的泄漏。

欲瞭解更多瞭解請參考內存指南或快速查看點擊http://www.raywenderlich.com/2657/memory-management-in-objective-c-tutorial

+0

爲什麼我會釋放pickerView? –

+0

它是全球性的,也不是本地的,那麼你如何說釋放它? –

+0

我想念那一點。如果實例是全局聲明的,則必須在dealloc方法中釋放。 – iamsult

相關問題