为什么 `-[UILabel setText:]` 会泄漏?
在我的 iPad 应用程序的 iOS 4.2.1 上的 UIScrollViewDelegate 类中,-scrollViewDidEndDecelerating:
方法调用另一个执行此操作的方法,此:
EntryModel *entry = [entries objectAtIndex:index];
self.titleLabel.text = entry.title;
title
是一个非原子的、保留的 NSString 属性入门模型。 titleLabel
是一个非原子的保留属性,通过 IBOutlet 将其连接到 nib 中定义的 UILabel。遵循 bbum 的 博文,我一直在使用Heapshot分析,并将上述代码识别为泄漏。几乎每次我滚动到新页面时, titleLabel
都会泄漏一点:
如果我将第二行更改为:
self.titleLabel.text = @"Whatever";
泄漏停止:
我很困惑。 -[UILabel text]
在分配新值之前是否不释放旧值?我假设不是,我一定做错了什么。为什么会漏这个?
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. Why does this leak?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许你实际上并没有泄漏内存。您正在分配内存,因为 UILabel 上的文本属性使用复制语义。因此,调用 self.titleLabel.text 将在赋值的右侧创建 NSString 的副本。尝试使用 Leaks 工具运行,看看是否存在内存泄漏。
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?