仪器显示内存泄漏 - Xcode 5 / iOS7
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19030069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Instruments show memory leak - Xcode 5 / iOS7
提问by rudy
I have the following piece of code:
我有以下一段代码:
NSString *bgImageName = [[Useful instance] getRootviewBackgroundImageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:bgImageName]];
imageView.clipsToBounds = YES;
CGRect rc = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[imageView setFrame:rc];
[self.view insertSubview:imageView atIndex:0];
[imageView release];
Instruments shows 100% and a memory leak at line two in the above code, which was not the case in xcode 4.6. I am working now with xCode 5 on osx 10.8.5
Instruments 在上述代码的第二行显示 100% 和内存泄漏,而 xcode 4.6 中并非如此。我现在在 osx 10.8.5 上使用 xCode 5
It seems, that i properly release the allocated UIImageView
(line 7) which gets inserted in my view at line 6, so i can't see why intruments is bringing up a memory leak warning.
看来,我正确释放了UIImageView
在第 6 行插入到我的视图中的分配(第 7 行),所以我不明白为什么仪器会发出内存泄漏警告。
Does anybody know why instuments brings up that (in my opinion) wrong information?
有谁知道为什么仪器会提出(在我看来)错误的信息?
EDIT: here's the instruments-screenshot with the allocation-summary of my leaked object:
编辑:这是我的泄漏对象的分配摘要的仪器屏幕截图:
The UIKit
and QuartzCore are retaining my object, which is the reason for my leaking UIImageView
(or am I wrong with this assumption?).
在UIKit
和QuartzCore被保留我的对象,这是我的泄漏原因UIImageView
(或我错了这个假设呢?)。
The UIImageView
is inserted to the view (a UIViewController
), which is referenced in my xib-file. How can control what happens with my added UIImageView
after adding it to 'self.view'?
将UIImageView
被插入到图(a UIViewController
),这是在我的XIB-文件引用。UIImageView
将其添加到“self.view”后如何控制我添加的内容?
回答by Dan Marinescu
I had the same issue with Xcode 5 on iOS 7. After some experimentation I noticed that Instruments does not show a mem leak when running against the iOS 6.1 simulator or against a device (iPhone 5s) running iOS 7. Based on these I can only conclude that this is a false positive and a bug in the iOS 7 simulator.
我在 iOS 7 上使用 Xcode 5 遇到了同样的问题。经过一些实验,我注意到 Instruments 在针对 iOS 6.1 模拟器或针对运行 iOS 7 的设备(iPhone 5s)运行时没有显示内存泄漏。基于这些,我只能得出结论,这是 iOS 7 模拟器中的误报和错误。
EDIT: This issue no longer occurs after I updated to Xcode 5.0.1 and OS X Mavericks (I'm guessing it's the first that fixed it but can't tell for sure).
编辑:在我更新到 Xcode 5.0.1 和 OS X Mavericks 后不再出现这个问题(我猜这是第一个修复它但不能确定的)。
回答by bbum
Instruments is showing the line of code that triggered the allocation of the leaked object and notwhy the object was actually leaked. Turn on reference count tracking and look at all the retain/release events on that image view. There'll be an extra retain (or a missing release).
Instruments 显示了触发泄漏对象分配的代码行,而不是实际泄漏对象的原因。打开引用计数跟踪并查看该图像视图上的所有保留/释放事件。会有一个额外的保留(或丢失的版本)。
回答by Rob
I notice that this is happening on your RootViewController
instance. Is this really the top level view controller (e.g. one that you never dismiss/pop)? If so, this Allocations Summary correctly telling you that the image view is still alive, and it's curious it's reported as a leak.
我注意到这发生在您的RootViewController
实例上。这真的是顶级视图控制器吗(例如,你从不解雇/弹出的)?如果是这样,这个 Allocations Summary 正确地告诉你图像视图仍然存在,并且奇怪的是它被报告为泄漏。
If the view controller has been dismissed, though, then you have a genuine leak (though the problem does not rest in the code in your question: you've done that correctly). Your Allocations Summary proves that your UIImageView
is never getting destroyed. If the view controller was correctly dismissed and deallocated, the image view's Allocations Summary should look like:
但是,如果视图控制器已被解除,那么您就有了真正的泄漏(尽管问题不在您问题中的代码中:您已经正确地做到了)。您的分配摘要证明您UIImageView
永远不会被破坏。如果视图控制器被正确解除和释放,图像视图的分配摘要应该如下所示:
Note, when I pop the view controller (28 seconds in my Profiling session), the view is getting deallocated, and thus the UIImageView
is, too (see the two highlighted lines).
请注意,当我弹出视图控制器(在我的分析会话中为 28 秒)时,视图将被释放,因此UIImageView
也是(参见突出显示的两条线)。
You can draw two conclusions from your Allocations Summary:
您可以从分配摘要中得出两个结论:
It doesn't show any other retains on your
UIImageView
suggests that this image view is not the problem at all. It is not the case that you accidentally over-retained this image view somewhere.The fact that you don't see the
removeFromSuperview
, suggests that yoursuperview
, itself, is never getting deallocated.
它不会在您的
UIImageView
建议中显示任何其他保留,即此图像视图根本不是问题。并非您不小心在某处过度保留了此图像视图。您看不到 的事实
removeFromSuperview
表明您的superview
本身永远不会被解除分配。
So, the question is whether your RootViewController
has been removed from the view controller hierarchy or not. If so, then you have a leak or retain cycle, probably of the view controller itself, not this image view.
因此,问题是您RootViewController
是否已从视图控制器层次结构中删除。如果是这样,那么您有一个泄漏或保留周期,可能是视图控制器本身,而不是这个图像视图。
By the way, it sounds like you're already familiar with these topics, but I often refer people to WWDC 2012 video iOS App Performance: Memory, which not only describes many memory issues, but also demonstrates the use of Instruments for tracking these down (e.g. notably heapshots).
顺便说一句,听起来您已经熟悉这些主题,但我经常向人们推荐 WWDC 2012 视频iOS App Performance: Memory,它不仅描述了许多内存问题,还演示了使用 Instruments 来跟踪这些问题(例如,特别是堆快照)。
回答by mishod
I started having the same issues and I was looking into imageNamed. I found that in the past there were memory issues. I finally ended up using [[UIImage alloc] initWithContentsOfFile:actualPath] where actualPath comes from the main bundle.
我开始遇到同样的问题,我正在研究 imageNamed。我发现过去有内存问题。我最终使用了 [[UIImage alloc] initWithContentsOfFile:actualPath] ,其中 actualPath 来自主包。
回答by Cortex
I think @mishod has the correct answer.
我认为@mishod 有正确的答案。
I tested and yes UIImageView setImage indeed leaks!
我测试过,是的 UIImageView setImage 确实泄漏了!
If you cycle through a bunch of images with
如果你循环浏览一堆图像
[yourImageView setImage:[UIImage imageNamed:@"sampleImage.png"]];
you can see on instruments memory usage increasing. This seems to be some kind of caching going around since after cycling through all the images memory usage will go flat.
您可以看到仪器内存使用量增加。这似乎是某种缓存,因为在循环浏览所有图像后,内存使用量将趋于平缓。
The correct, or at least, the non leaky way to do it is:
正确的,或者至少是非泄漏的方法是:
NSString *thePath = [[NSBundle mainBundle] pathForResource:@"sampleImage" ofType:@"png"];
UIImage *newImage = [[UIImage alloc] initWithContentsOfFile:thePath];
[yourImageView setImage:newImage];
I verified this on my code as my APP was cycling through a lot of large image files.
我在我的代码中验证了这一点,因为我的 APP 正在循环浏览大量大图像文件。
回答by Gautam Jain
Just a suggestion. Don't run Instruments on simulator. Do it on a device. There's a lot of difference and you see better/relevant results. Eg. Simulator has a lot more memory than a real device may have.
只是一个建议。不要在模拟器上运行 Instruments。在设备上进行。有很多不同,你会看到更好/相关的结果。例如。模拟器的内存比真实设备多得多。