打开UIPickerController并关闭它时内存泄漏

时间:2011-11-05 12:06:58

标签: objective-c ios uipickerview

这可能听起来像一个新手问题,但我是iOS开发新手。

我已经关注了代码。

- (void) onUploadButtonClick
{
    UIImagePickerController* imgPicker = [[UIImagePickerController alloc] init];
    [[[UIApplication sharedApplication] keyWindow] setRootViewController:imgPicker];
    imgPicker.delegate = self;
    imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    imgPicker.allowsEditing = NO;
    [self presentModalViewController:imgPicker animated:YES];
    [imgPicker release];
}

我正在运行应用程序并对内存泄漏进行分析,所以只需单击按钮并关闭它,而不做任何事情我会得到内存泄漏。我在模拟器上运行它。

为什么会这样?

更新:来自探查器控制台的泄漏信息 泄露的对象,#地址大小负责的图书馆负责框架

Malloc 32.50 KB,3   < multiple >    99840   MusicLibrary    MemNewPtrClear
 Malloc 32.50 KB,   0xa083800   33280   MusicLibrary    MemNewPtrClear
 Malloc 32.50 KB,   0x7840a00   33280   MusicLibrary    MemNewPtrClear
 Malloc 32.50 KB,   0x7806a00   33280   MusicLibrary    MemNewPtrClear


Leaked Object,# Address Size    Responsible Library Responsible Frame
Malloc 32.50 KB,    0xa083800   33280   MusicLibrary    MemNewPtrClear


Leaked Object,# Address Size    Responsible Library Responsible Frame
Malloc 32.50 KB,    0x7840a00   33280   MusicLibrary    MemNewPtrClear


Leaked Object,# Address Size    Responsible Library Responsible Frame
Malloc 32.50 KB,    0x7806a00   33280   MusicLibrary    MemNewPtrClear


Leaked Object,# Address Size    Responsible Library Responsible Frame
Malloc 128.00 KB,   0x128de000  131072  MusicLibrary    ReadITImageDB

2 个答案:

答案 0 :(得分:1)

为什么你会用UIImagePickerController做类似的事情?你实际上是在杀死你的实际rootViewController。

 [[[UIApplication sharedApplication] keyWindow] setRootViewController:imgPicker];

只需删除此行,一切都会正常。

答案 1 :(得分:1)

您应该使用UINavigationController并将UIImagePickerController推到其上,或以模拟方式显示UIImagePickerController。通过将UIImagePickerController设置为您的根控制器,您将丢失之前的rootViewController,并且无法返回它。内存泄漏可能是因为根UIViewController错误地实现了viewDidUnload和dealloc方法。