核心文本内存分配问题 [英] Core Text Memory allocation issue

查看:82
本文介绍了核心文本内存分配问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用分配工具检查了我的应用程序,发现此处的这段代码导致我分配问题.该方法返回一个正方形区域的建议高度,该正方形区域填充有传递的属性字符串;我需要这样做,以便计算需要多少空间来绘制文本并生成书页:

I inspected my app with the allocation instrument and I discovered that this code down here cause me an allocation issue. The method returns the suggested height of a squared area filled with the passed attributed string; I need this in order to calculate how much space I need to draw that text and then generating book pages:

- (CGFloat)boundingHeightForWidth:(CGFloat)inWidth ForAttributedString:(NSAttributedString *)attributedString
{
     CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFMutableAttributedStringRef)attributedString);
     CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, CGSizeMake(inWidth, 10000), NULL);
     CFRelease(framesetter);
     return suggestedSize.height ;
}

由于我在流程中多次调用此方法,所以我想知道这是如何导致多达7MB的内存分配的.我以为释放帧设置器就足够了,我错了吗?

Since I am calling this method many and many times during the flow, I am wondering how this is causing up to 7MB of memory allocation.. I thought releasing the frame setter could be enough, am I wrong?

推荐答案

我对此问题进行了详细的WRT调试,您可以在我对

I did some detailed debugging WRT to this issue, you can find the results in my answer to this question. There are a couple of things you could try. 1, does you app do these allocations in a secondary thread, if yes then does moving them to the main thread make the lost memory go away? Two, you could hold on to the CTFramesetterRef and then invoke CTFramesetterSuggestFrameSizeWithConstraints over and over with the same framesetter. The leak appears to be in the CTFramesetterCreateWithAttributedString() call, so perhaps you could minimize the leak by not invoking that method so many times.

这篇关于核心文本内存分配问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆