1

une question Noob ici.deux fuites de mémoire avec UIImagePickerController et UIPopoverController

J'ai le code suivant:

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

mais le compilateur me met en garde contre deux éventuelles fuites de mémoire. un pour imagePicker et l'autre pour popVC. Quelqu'un pourrait-il expliquer ce qui doit être changé et pourquoi? Je voudrais vraiment comprendre pourquoi cela se passe, donc je peux l'éviter à l'avenir.

merci!

Répondre

3

Vous ne libérez pas imagePicker ou popVC n'importe où, c'est pourquoi votre fuite. Vous pouvez ajouter une autorelease ou libérer quelque part là-dedans pour ceux.

Choisissez l'une des méthodes suivantes:

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

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

ou

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

[popVC release]; 

[imagePicker release]; 

Notez aussi comment vous avez utilisé [alert release];. Même concept

+0

merci! Je comprends maintenant. J'ai ajouté la publication automatique à l'imagePicker et à l'alerte, mais j'ai eu le code une version manuelle dans une méthode pour popVC (parce qu'il a été libéré automatiquement tout en se relevant et en plantant l'application) – TrekOnTV2017

Questions connexes