2011-11-16 11 views
0

次のコードを参照してください。これは単なる方法であり、メモリ管理の問題を理解できず、私に教えてください。次のコードで何が起きていますか?ここProduct> Analyze in 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]; 

}

、どのように私はこのコードでは適切なメモリ管理を行うことができますし、あなたが指定することができます

[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]]; 

ライン上で、このようなMSGを示しているPotential leak of object allocated on line 112 and stored into close button

を言っていますそれは詳細

ありがとう

答えて

1

まず、インスタンスpickerViewとcloseButtonの両方を解放する必要があります。

ルールごとに、オブジェクトを割り当てるときはいつでも、ジョブが完了したらオブジェクトを解放する必要があります。インスタンスをサブビューに追加した後のケースでは、それらは解放する必要がありません。それ以外の場合は、あなたが述べたようにリークを表示します。より多くの理解のために

はメモリガイドを参照するか、迅速なためhttp://www.raywenderlich.com/2657/memory-management-in-objective-c-tutorial

+0

は、なぜ私はpickerViewをリリースする予定クリック見えますか? –

+0

それはグローバルでもローカルでもなく、それをどのように解放すると言いますか? –

+0

私はその点を逃しています。インスタンスがグローバルに宣言されている場合は、deallocメソッドで解放する必要があります。 – iamsult

関連する問題