Why Does `-[UILabel setText:]` Leak?
In a UIScrollViewDelegate class on iOS 4.2.1 in my iPad app, the -scrollViewDidEndDecelerating:
method calls another method that does, this:
EntryModel *entry = [entries objectAtIndex:index];
self.titleLabel.text = entry.title;
title
is a nonatomic, retained NSString property of EntryModel. titleLabel
is a nonatomic, retained property with an IBOutlet connecting it to a UILabel defined in a nib. Following bbum's blog post, I've been using Heapshot analysis and have identified the above code as a leak. Nearly every time I scroll to a new page, titleLabel
leaks a bit:
If I change that second line to:
self.titleLabel.text = @"Whatever";
The leaking stops:
I'm confused. Is -[UILabel text]
not releasing old values before assigning new values? I'm assuming not, that I must be doing something wrong开发者_JS百科. Why does this leak?
Maybe you're not actually leaking memory. You are allocating memory, as the text property on a UILabel uses copy semantics. So, calling self.titleLabel.text
will create a copy of NSString on the right-hand side of the assignment. Try running with the Leaks instrument to see if you are leaking memory.
Given that you have heapshot generations with zero allocations, it isn't a consistent accretion of memory. It might be caching [gone wrong] or it might be a leak related to scrolling, something falling through the cracks in the events.
What do the heapshot iterations with allocations in them show?
精彩评论