Search code examples
cocoa-touchmemory-leaksuilabeluiscrollviewdelegate

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:

alt text

If I change that second line to:

self.titleLabel.text = @"Whatever";

The leaking stops:

alt text

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. Why does this leak?


Solution

  • 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?