我有以下用于缩放图像的代码片段.这是在一个循环中,为每次传递创建和排出一个自动释放池.此代码在 iOS5.0 的模拟器、iPad 上的 iOS4.3 或模拟器中运行良好,但在 iPad1 上的 iOS5.0.1 上,经过 50-60 次后,drawInRect 开始消耗永远不会释放的内存.我一直在从辅助线程调用它,但现在在主线程上调用缩放操作.
I have the following code fragment that I use to scale images. This is in a loop that creates and drains an autorelease pool for each pass. This code works fine in the simulator in iOS5.0, in iOS4.3 on iPad or simulator, but on iOS5.0.1 on an iPad1, after 50-60 passes, drawInRect starts consuming memory that never gets released. I had been calling this from a secondary thread, but now invoke the scaling operations on the main thread.
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
是我做错了什么还是 iOS5.0.1 的错误?
Am I doing something wrong or is this an iOS5.0.1 bug?
更新:我已经尝试了很多测试.我已经能够证明用 XCode 4.0 编译的完全相同的代码在同一台 iPad 上运行良好.使用 XCode 4.2.1 编译的相同代码会导致内存不足.此缩放例程正在后台线程中调用.我使用较低级别的核心图形调用编写了一个不同的缩放例程.XCode 4.0 不会泄漏,但在我的 XCode 4.2.1 应用程序中会泄漏.在独立项目中运行的完全相同的例程和调用树似乎不会在 XCode 4.2.1 上泄漏(大量)内存.我正在等待 Apple 关于这个的消息.同时,我需要使用 XCode 4.我拥有的唯一安装映像需要 Snow Leopard,这意味着我正在使用我的 5.5 年旧机器.谢谢
Update: I've tried many tests. I have been able to prove that the exact same code compiled with XCode 4.0 runs fine on the same iPad. Same code compiled with XCode 4.2.1 causes an out of memory condition. This scaling routine is being invoked in a background thread. I wrote a different scaling routine using lower level core graphics calls. It doesn't leak with XCode 4.0 but does leak when in my application with XCode 4.2.1. The exact same routine and invocation tree run in a stand alone project does not appear to leak (much) memory on XCode 4.2.1. I'm waiting to hear from Apple on this one. In the mean time I need to use XCode 4. The only install image I have requires Snow Leopard which means I'm using my ancient 5.5 yr old machine. Thanks
2012 年 1 月更新这似乎仅在应用程序从 XCode 中启动时才会发生.在 iPad 上启动的相同可执行文件没有泄漏.具有相同例程的不同应用不会出现泄漏.
Update 1/2012 This only seems to occur if the app is started from within XCode. Same executable started on the iPad does no exhibit the leak. A different app with the same routine does not exhibit the leak.
我已向 Apple 打开了一份错误报告,并向他们发送了一个重现该问题的项目.我预计它不会很快得到解决,但它似乎并不像我最初想象的那样普遍.
I've opened a bug report with Apple and have sent them a project which reproduces the problem. I don't expect it to be resolved anytime soon, but it doesn't seem as pervasive as I originally thought.
2012 年 6 月更新尽管已向 Apple 发送了一个重现该问题的最小项目,但他们声称无法重现该问题并且没有取得任何进展.
Update 6/2012 Despite having sent Apple a minimal project which reproduced the problem, they claim not to be able to reproduce the problem and are not making any progress on it.
我相信我终于找到了内存泄漏的原因.在进行一些核心数据插入时,我发现了类似的行为.循环,创建许多被释放的对象.在 iPad 上运行时,内存使用量会上升,但在应用程序因内存不足而崩溃之前没有明显的泄漏.但是当从设备启动时,它运行没有问题.
I believe I have FINALLY found the cause of the memory leak. I discovered similar behavior when doing some Core Data inserts. Looping, creating many objects that are released. When run on iPad, memory usage goes up although no leak is evident until the app crashes out of memory. But when initiated from the device, it runs without a problem.
我突然想到它是从 Xcode 开始的.必须是项目中的调试设置.
It occurred to me that its something about the way its being started from Xcode. Must be a debug setting in the project.
原来问题是由于在调试时使用 NSZombieEnabled
引起的.要在 Xcode 4 中禁用此设置,请右键单击方案,即 app>targetDevice,编辑方案,选择调试操作,参数选项卡.要启用 NSZombieEnabled
,需要使用该名称创建一个值为 YES 的环境变量,并启用该变量.要禁用它,请取消选中该复选框.
Turns out the problem was caused by having NSZombieEnabled
while debugging. To disable this setting in Xcode 4, right click on the schemes, ie app>targetDevice, edit the scheme, select the Debug action, arguments tab. To enable NSZombieEnabled
, an environment variable is created with that name with a value of YES and the variable is enabled. To disable it, uncheck the checkbox.
NSZombieEnabled
用于确定您是否尝试释放已释放的对象.为此,环境会跟踪所有已发布的对象.这会消耗内存,表现为内存泄漏.
NSZombieEnabled
is used to determine if you attempt to release an object that has already been released. To do this, the environment is keeping track of all released objects. This is consuming memory which appears as a memory leak.
禁用此功能后,我的应用程序在 iPad1 上被杀死之前会快速增长超过 115MB,现在可以愉快地坐在 24MB 上,没有内存泄漏.
Once I disabled this, my app that used to quickly grow over 115MB before being killed on an iPad1 now happily sits at 24MB with no memory leak.
这篇关于iOS5.0.1 上的内存泄漏 drawInRect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!