内存使用增长与CTFontCreateWithName和CTFramesetterRef [英] Memory usage grows with CTFontCreateWithName and CTFramesetterRef

查看:1890
本文介绍了内存使用增长与CTFontCreateWithName和CTFramesetterRef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个使用自定义字体的IOS程序(CTFontManagerRegisterFontsForURL)。我加载字体,将其添加为一个字符串属性,创建一个framesetter,然后一个框架,并绘制到上下文。
我释放我使用的一切。仪器不会注意到泄漏,但:

使用此功能时,应用程序所使用的内存会增长并且不会收缩。
当我离开函数时,字体的保留计数是2。

  CFMutableAttributedStringRef 
CFAttributedStringBeginEditing(attributedStringRef);
CFAttributedStringReplaceString(attributedStringRef,CFRangeMake(0,0),(CFStringRef)label.text);

font = CTFontCreateWithName((CFStringRef)label.fontName,label.fontHeight,NULL);

保留字数:1

  CFAttributedStringSetAttribute(attributedStringRef,CFRangeMake(0,label.text.length),kCTFontAttributeName,font); 
CFAttributedStringEndEditing(attributedStringRef);

保留字数:2

  CGMutablePathRef path = CGPathCreateMutable(); 
CGPathAddRect(path,NULL,rect);

CFRelease(font);

保留字数:1

  CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(attributedStringRef); 

保留字数:3

  CFRelease(attributedStringRef); 
CTFrameRef frame = CTFramesetterCreateFrame(frameSetter,
CFRangeMake(0,0),
path,NULL);

保留字体数量:5

  CFRelease(frameSetter); 

保留字数:4

  CTFrameDraw(frame,ctx); 
CFRelease(frame);

保留字数:2

  CGPathRelease(路径); 

是否有某种缓存?我真的需要立即刷新这个字体的内存。



PS:我使用CFGetRetainCount来获取字体的保留数。

谢谢!

解决方案

retainCount没用。不要调用它。



如果您的应用程序的内存以可重复的方式增长,请使用堆积分析弄清楚什么是消耗内存。泄漏只报告不再可到达的对象 - 地址没有出现在任何活动的内存区域中的对象 - 因此,泄漏将不会找到多种内存增加。



这可能是只写缓存的情况;即在某个地方积极地缓存东西,但是你的代码被写入,使得缓存的副本从不被检索。没有额外的信息 - 对于初学者来说,Heapshot分析的结果 - 很难说。
$ b $ hr

lockquote

我跟着你的教程,它确认永久堆
的增长是由于CTFramesetterRef frameSetter =
CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string(
)字符串)。
确定 - 您已经确认了什么是泄漏和分配的位置,但是没有额外的保留来自哪里。为此,打开分配工具中的记录引用计数并重新运行测试。这将允许您检查违规对象上的每个保留/释放调用的回溯。那里会有额外的保留;一个保留不平衡的发布。

我猜上下文以某种方式挂在它上面。


(我已经分析过内存,看到它被
占用了这个对象,所以我检查了保留数。


对象的绝对保留数是无用的,它仍然在内存中意味着它被过度保留,保留数本身不能真的告诉你更多的东西,除非你还完全回溯了对象上的每一个保留(和释放)调用,这些仪器给你。

I'm writing an IOS program which uses custom fonts (CTFontManagerRegisterFontsForURL). I load the font, add it as a string attribute, create a framesetter, then a frame, and draw it to a context. I release everything i use. Instruments doesn't notice a leak but :

The memory used by the applications grows and doesn't shrink when using this function. The retain count of my font is 2 when i leave the function.

Here is the code :

CFMutableAttributedStringRef attributedStringRef = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CFAttributedStringBeginEditing(attributedStringRef);
CFAttributedStringReplaceString(attributedStringRef, CFRangeMake(0, 0), (CFStringRef)label.text);

font = CTFontCreateWithName((CFStringRef)label.fontName, label.fontHeight, NULL);

retain count of the font : 1

CFAttributedStringSetAttribute(attributedStringRef, CFRangeMake(0, label.text.length), kCTFontAttributeName, font);
CFAttributedStringEndEditing(attributedStringRef);

retain count of the font : 2

CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, rect);

CFRelease(font);

retain count of the font : 1

CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(attributedStringRef); 

retain count of the font : 3

CFRelease(attributedStringRef);
CTFrameRef frame = CTFramesetterCreateFrame(frameSetter,
                                            CFRangeMake(0, 0),
                                            path, NULL);

retain count of the font : 5

CFRelease(frameSetter);

retain count of the font : 4

CTFrameDraw(frame, ctx);
CFRelease(frame);

retain count of the font : 2

CGPathRelease(path);

Is there some sort of cache ? I really need to flush the memory used by this font immediately.

P.S : I used CFGetRetainCount to get the retain count of the font.

Thanks !

解决方案

retainCount is useless. Don't call it.

If your app's memory is growing in a repeatable fashion, use Heapshot Analysis to figure out what is consuming memory. Leaks only reports objects that are no longer reachable -- objects whose address does not appear in any active regions of memory -- and, thus, leaks will not find many kinds of memory accretion.

This may be a case of a write-only cache; i.e. something somewhere is proactively caching stuff, but your code is written such that the cached copies are never retrieved. Without additional information -- the results of Heapshot Analysis, for starters -- it is hard to say.


I followed your tutorial, and it confirms that the permanent heap growth is due to the line "CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string); ". OK -- you've confirmed what is leaking and where it is allocated, but not where the extra retain comes from. To that, turn on "Record reference counts" in the Allocations instrument and re-run the test. This will allow you to inspect the backtraces of every retain/release call on the offending object. There will be an extra retain in there; a retain not balanced by a release.

I'm guessing the context is somehow hanging on to it.

(I had already analyzed the memory and saw that it was occupied by this object, that's why i checked retain count.

The absolute retain count of an object is useless. That it is still in memory means that it is over-retained and the retain count, itself, can't really tell you anything more unless you also have the full backtrace of every single retain (and release) call on the object, which Instruments gives you.

这篇关于内存使用增长与CTFontCreateWithName和CTFramesetterRef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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