本文介绍了uiimagepickerview控制器在iPhone中创建内存泄漏 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

uiimagepickerview控制器在iphone中创建内存泄漏 - 为什么?

uiimagepickerview controller creating memory leaks in iphone - why?

尝试在您的应用程序中实现ui图像选择器视图控制器&调试它。
您将在应用程序中发现内存泄漏。
为什么ui图像选择器视图控制器会造成内存泄漏。

Try to implement ui image picker view controller in your application & debug it.You will find memory leaks in your application.Why ui image picker view controller creates memory leaks.

-(void)addPhotos:(id)sender
{
    if(imagePickerController==nil){ 
          imagePickerController=[[UIImagePickerController alloc]init];
          imagePickerController.delegate=self;
          imagePickerController.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        imagePickerController.allowsImageEditing=YES;
          imagePickerController.navigationBar.barStyle=UIBarStyleBlackOpaque;
    }
[self.navigationController presentModalViewController:imagePickerController animated:YES];
}

我的视图控制器dealloc。

dealloc of my view controller.

- (void)dealloc {
if(PhotoDateArray!=nil)[PhotoDateArray release];
if(imagePickerController!=nil) [imagePickerController release];
if(objDetail!=nil) [objDetail release];
if(Picimage!=nil) [Picimage release];
if(mySavePhotoController!=nil) [mySavePhotoController release];
if(LoadingAlert!=nil);
[super dealloc];
}

视频链接解释我如何获取内存泄漏..

Video link explaining how I am getting the memory leak in it..

推荐答案

即使你有零检查,它仍然可能泄漏记忆。我认为这里发生的事情是你多次调用alloc / init,但只发布一次。我猜它 addPhoto:连接到某个按钮点击,dealloc只会在委托试图销毁时调用一次。这会产生如下情况:

Even though you have the nil check, it's still possible to leak memory. I think what is happening here is that you are calling alloc / init multiple times, but only releasing once. My guess it that addPhoto: is wired up to some button click, dealloc would only be called once when the delegate is trying to destroy. This creates a situation like this:


  • 按钮点击


    • alloc / init


    • alloc / init(内存)第一次分配选择器泄漏)


    • dealloc(免费第二次分配选择器)

    更好的方法可能是Apple在和示例:

    A better way might be the way Apple does it in the PhotoLocations and iPhoneCoreDataRecipes examples:

    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    [self presentModalViewController:imagePicker animated:YES];
    [imagePicker release];
    

    然后听 didFinishPickingImage imagePickerControllerDidCancel 给你的委托的消息以及对这两个地方的 [self dismissModalViewControllerAnimated:YES]; 的调用就足够了。

    Then listen for the didFinishPickingImage and imagePickerControllerDidCancel messages to your delegate and a call to [self dismissModalViewControllerAnimated:YES]; in both places should suffice.

    这篇关于uiimagepickerview控制器在iPhone中创建内存泄漏 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 22:37