ios - sizeWithFont 内存泄漏 :constrainedToSize:lineBreakMode:

标签 ios objective-c memory-leaks instruments

尝试创建 CGSize 来设置 UILabel 的正确高度时出现泄漏。在 heightForRowAtIndexPath 中设置高度时,我也收到相同的泄漏。

这是泄漏的代码片段:

CGSize size = [news.news sizeWithFont:[UIFont fontWithName:@"HelveticaNeue" size:12] constrainedToSize:CGSizeMake(230.0f, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];

                UILabel *newsLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, 50, 230, size.height)];
                newsLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:12];
                newsLabel.textAlignment = NSTextAlignmentLeft;
                newsLabel.text = news.news;
                newsLabel.numberOfLines = 0;
                newsLabel.lineBreakMode = NSLineBreakByWordWrapping;
                newsLabel.textColor = COLOR_DARK_GRAY;
                newsLabel.highlightedTextColor = COLOR_WHITE;
                newsLabel.backgroundColor = COLOR_CLEAR;
                [cell.contentView addSubview:newsLabel];
                [newsLabel release];

这是泄漏工具中列出的泄漏:

泄露的对象:icu::UCharCharacterIterator 负责库:WebCore 责任框架:WebCore::LineBreakIteratorPool::take(WTF::AtomicString const&)

还有一个指向同一行的不同泄漏:

泄露的对象:icu::UCharCharacterIterator 负责库:WebCore 责任框架:WebCore::acquireLineBreakIterator(unsigned Short const*, int, WTF::AtomicString const&)

如果我还能提供任何其他信息,我很乐意这样做。我通过注释掉上面的单行(创建 CGSize 大小)来确认这是泄漏的行。在模拟器和设备上都会发生。

最佳答案

在您的语句 newsLabel.font = [UIFont fontWithName:@"HelveticaNeue"size:12]; 中,您创建一个要自动释放的 UIFont 对象。但是,如果您的代码在没有使用 @autoreleasepool {} 显式设置自动释放池的线程中运行,则该对象将永远不会被释放(因为不存在自动释放池),并且会泄漏。
因此,如果您的代码确实在单独的线程中运行,请检查您是否设置了自动释放池。

关于ios - sizeWithFont 内存泄漏 :constrainedToSize:lineBreakMode:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14676705/

相关文章:

ios - UIViewController子类策略

ios - 给定分发包ID,可以在iOS 7上获取identifierForVendor(IDFV)吗?

ios - NSFileSystemFreeSize 返回不准确的可用磁盘空间?

objective-c - NSCollectionView 什么都不画

ios - 如何修复 _NSCFNumber 的 iOS 内存泄漏?

android.widget.PopupWindow$PopupDecorView 在打开菜单的情况下旋转设备时泄漏

asp.net - 在 ASP.NET Web App 中查找内存泄漏

ios - 在 Swift 2.0 中使用 MFMailComposeViewController

iphone - 更改 IOS 应用程序上 TabBar 应用程序中的当前屏幕

objective-c - iOS:UIView 的简单物理