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

发布于 2024-12-13 20:18:27 字数 1535 浏览 2 评论 0原文

这听起来可能是一个新手问题,但我是 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

This may sound a newbie question, however I'm new to iOS dev.

I've following code.

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

I'm running the app and profiling for memory leaks, so by just clicking on the button and closing it, without doing anything I am getting memory leak. I'm running this on simulator.

Any ideas why this happens ?

UPDATE : Leak info from profiler's console
Leaked Object,# Address Size Responsible Library Responsible Frame

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

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

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

发布评论

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

评论(2

四叶草在未来唯美盛开 2024-12-20 20:18:27

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

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

只要删除这一行,一切都会正常工作。

Why would you ever do something like that with a UIImagePickerController? You're literally killing your actual rootViewController.

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

Just remove this line and everything will work fine.

李白 2024-12-20 20:18:27

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

You should use a UINavigationController and push the UIImagePickerController onto it, or present the UIImagePickerController modally. By setting the UIImagePickerController as your root controller you are loosing the previous rootViewController and will not be able to return to it. The memory leak could be due to the fact that that the root UIViewController has incorrectly implemented viewDidUnload and dealloc methods.

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