问题
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
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.
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?).
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'?
回答1:
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.
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).
回答2:
Instruments is showing the line of code that triggered the allocation of the leaked object and not why 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).
回答3:
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.
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:

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).
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.
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.
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).
回答4:
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.
回答5:
I think @mishod has the correct answer.
I tested and yes UIImageView setImage indeed leaks!
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.
回答6:
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.
来源:https://stackoverflow.com/questions/19030069/instruments-show-memory-leak-xcode-5-ios7