UIImagePickerController 的内存泄漏问题

发布于 2024-07-11 23:42:00 字数 799 浏览 6 评论 0原文

我的 UIImagePickerController 类出现内存泄漏。

以下是我使用它的方式:

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

要删除选择器,我在 didFinishPickingImageimagePickerControllerDidCancel 中调用 [picker DismissModalViewControllerAnimated:YES];

--

由于此指令,仪器显示大约 160 字节泄漏:

+[UIImagePickerController _loadPhotoLibraryIfNecessary]

显然这个问题已经并且正在困扰很多人,以及解决方案 避免这个问题的方法是建立一个 专门用于挑选的单例类 来自库的图像或使用捕获的图像 设备内置摄像头。

有人想添加一些东西吗?

I'm getting memory leak with UIImagePickerController class.

Here's how I'm using it:

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

To remove the picker i call [picker dismissModalViewControllerAnimated:YES]; in didFinishPickingImage and imagePickerControllerDidCancel.

--

Instruments show around 160bytes leaking as a result of this instruction:

+[UIImagePickerController _loadPhotoLibraryIfNecessary]

Apparently this issue has and is disturbing many people, and solution
to avoid this problem is to build a
singleton class dedicated for picking
images from library or capturing using
device's build in camera.

Anyone want to add something?

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

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

发布评论

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

评论(5

筑梦 2024-07-18 23:42:00

作为的作者之一第一篇文章介绍了使用单例的必要性,其动机是为了防止在 7/8 图像捕获时发生崩溃,而不是因为对泄漏有任何特别的担心。 160 字节很烦人,但不是主要问题,因此不值得担心(因为开发人员无法修复它)。

As the author of one of the first articles about the necessity to use a singleton, the motivation was to prevent a crash on the 7/8th image capture, not because of any particular worry about the leak. 160 bytes is annoying, but not a major problem, and therefore not worth worrying about (because it can't be fixed by developers).

心是晴朗的。 2024-07-18 23:42:00

您是否尝试过删除委托线? 当委托给 self 时,我在 AVAudioPlayer 方面也遇到了类似的问题。 (即使访问器在两种情况下都说分配。)如果泄漏随着委托而消失,您可以委托给另一个对象。

Have you tried deleting the delegate line? I’ve had similar problems with AVAudioPlayer when delegating to self. (Even though the accessor says assign in both cases.) If the leak goes away with the delegation, you can delegate to a different object.

家住魔仙堡 2024-07-18 23:42:00

我在 Instruments 中发现了内存分配泄漏。
我所做的只是打开和关闭图像选择器(打开/取消)并使用 Apple 代码、我的代码和其他人的代码。

每次都显示分配量不断增加,就好像拾取器没有被释放一样。
如果您尝试释放它,它会崩溃(过度释放)。

然后我发现了一个非常有用的网页,基本上说:

“在设备上测试时不会发生这种情况”,

所以我从模拟器切换并在设备上运行测试。
罗& 看到没有分配增加并且表现正常。

然而,这是完全邪恶的,现在我们不能相信模拟器能够完成可靠的工作。 无论这是否与您的具体问题相关,我都会向您提出其他要添加的内容,而我要添加的内容是不要在模拟器上测试内存!

I was having a memory alloc leak which I found in Instruments.
All I was doing was opening and closing the image picker (open/cancel) and using Apple code, my code and other people's code.

All were showing the allocation going up and up each time, as if the picker was not being released.
If you tried to release it, it would crash (over released).

Then I found a really helpful web page which basically stated:

"This doesn't happen when testing on the device"

So I switched from the simulator and ran the tests on the device.
Lo & behold there was no allocation increase and it behaved normally.

This however is totally evil and now we can place no trust in the simulator to do a reliable job. Whether this is pertinent to your specific problem or not, I took you up on anything else to add, and my thing to add is don't test memory on the simulator!

信仰 2024-07-18 23:42:00

原因可能是您忘记释放图像。 因为每次编写

UIImageView.image = image_a;

Then 时,image_a 都会被保留一次。

直到你让UIImageView.image = nil,当image_a终于可以释放的时候。

我用这种方式解决了我的问题。

The reason maybe that you forget to release image. Because each time you write

UIImageView.image = image_a;

Then , image_a will get retained once.

Until you let UIImageView.image = nil, when image_a can be release finally.

I resolved my problem in this way.

紫﹏色ふ单纯 2024-07-18 23:42:00

如果您使用 UIImagePickerController 发现多个 GeneralBlockSegmentMachO 内存泄漏,

请尝试添加 CoreLocation 框架和 MapKit 框架你的项目。 我在仪器工具泄漏检查中没有看到更多内存泄漏。 我不知道 UIImagePickerController 与这些框架有何关系。 我不确定这是否是好的解决方案。 “在不使用或没有必要的情况下添加框架”。

我还通过使用 UIImagePickerController 发生了内存泄漏。 即使在从 developer.apple.com 下载的示例代码“PhotoLocation”和“iPhoneCoreDataRecipes”中,也会发生内存泄漏。 我还通过将这些框架添加到下载的示例代码中进行了检查。 不再有内存泄漏。

If you see memory leaks several GeneralBlock and SegmentMachO by using UIImagePickerController,

Try by adding CoreLocation framework and MapKit framework to your project. I don't see anymore memory leaks in the instrument tool leak checking. I don't know how UIImagePickerController related to these frameworks. I am not sure it is good solution or not. "adding frameworks without using or necessary".

I have also got the memory leak by using UIImagePickerController. That memory leak happen even in the sample code "PhotoLocation" and "iPhoneCoreDataRecipes" downloaded from developer.apple.com. I also checked by adding these frameworks to those downloaded sample code. There is no memory leaks anymore.

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