UIImagePickerController 和 UIPopoverController 的两次内存泄漏

发布于 2024-10-08 01:02:23 字数 1105 浏览 0 评论 0原文

这是一个菜鸟问题。

我有以下代码:

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

但编译器警告我两个潜在的内存泄漏。一个用于 imagePicker,另一个用于 popVC。有人可以解释一下需要改变什么以及为什么。我真的很想了解为什么会发生这种情况,这样我以后就可以避免这种情况。

谢谢你!

a noob question here.

I have the following code:

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

but the compiler warns me about two potential memory leaks. one for imagePicker and the other for popVC. could someone please explain what needs to be changed and why. I'd really like to understand why this is happening, so I can avoid it in the future.

thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月寒剑心 2024-10-15 01:02:23

您没有在任何地方发布 imagePickerpopVC,这就是您泄漏的原因。您可以在其中的某个位置添加自动发布或发布。

选择以下方法之一:

/*  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];

另外,请注意您如何使用[警报发布];。相同的概念。

Your not releasing imagePicker or popVC anywhere, thats why your leaking. You can add an autorelease or release somewhere in there for those.

Choose one of these methods:

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

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

or

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

[popVC release];

[imagePicker release];

Also, notice how you've used [alert release];. Same concept.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文