UIImagePickerController 和 UIPopoverController 的两次内存泄漏
这是一个菜鸟问题。
我有以下代码:
- (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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有在任何地方发布
imagePicker
或popVC
,这就是您泄漏的原因。您可以在其中的某个位置添加自动发布或发布。选择以下方法之一:
或者
另外,请注意您如何使用
[警报发布];
。相同的概念。Your not releasing
imagePicker
orpopVC
anywhere, thats why your leaking. You can add an autorelease or release somewhere in there for those.Choose one of these methods:
or
Also, notice how you've used
[alert release];
. Same concept.