1

ここにnoobの質問があります。UIImagePickerControllerとUIPopoverControllerで2つのメモリリークが発生する

私は、次のコードを持っている:

- (IBAction)selectExistingPicture 
{ 
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]) 
{ 
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 
    imagePicker.delegate = self; 
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 

    UIPopoverController *popVC = [[UIPopoverController alloc] initWithContentViewController: imagePicker]; 
    popVC.delegate = self; 
    [popVC setPopoverContentSize:CGSizeMake(320, 100)]; 
    [popVC presentPopoverFromRect:CGRectMake(39, 356, 320, 100) inView:self.view permittedArrowDirections:1 animated:NO]; 
} 
else 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error accessing photo library" 
       message:@"Device does not support a photo library" delegate:nil 
      cancelButtonTitle:@"Cancel" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
} 
} 

をコンパイラは二つの潜在的なメモリリークについて私に警告します。 1つはimagePicker用、もう1つはpopVC用です。変更が必要な理由とその理由を説明してください。私は本当になぜこれが起こっているのか理解したいので、将来それを避けることができます。

ありがとうございました!

答えて

3

あなたがどこにいなくてもimagePickerまたはpopVCをリリースしないと、あなたはそれらのためにそこにautoreleaseまたはリリースを追加することができます。

これらのメソッドのいずれかを選択します、また

/* this is the method I would suggest */ 
UIPopoverController *popVC = [[[UIPopoverController alloc] initWithContentViewController: imagePicker] autorelease]; 

UIImagePickerController *imagePicker = [[[UIImagePickerController alloc] init] autorelease];  

または

/* with these, you could potentially over-release somewhere, so be careful */ 

[popVC release]; 

[imagePicker release]; 

をあなたは[alert release];を使用しましたか気づきます。同じ概念。

+0

ありがとうございます!わかった。私はimagePickerへの自動リリースと警告を追加しましたが、popVCのメソッドでは手動でコードをリリースしていました(自動リリースしていてアプリがクラッシュしていたためです) – TrekOnTV2017

関連する問題