简体   繁体   English

CTFramesetterCreateWithAttributedString导致内存泄漏

[英]CTFramesetterCreateWithAttributedString cause memory leak

I don't know why this simple piece of code can cause memory leak sometimes (not always). 我不知道为什么这段简单的代码有时会导致内存泄漏(并非总是如此)。

This piece of code is wrapped in an NSOperation and runs in an NSOperationQueue queue. 这段代码包装在NSOperation中,并在NSOperationQueue队列中运行。 The operation will trim sourceNSAString to fit in some size and return it to some other thread as a result. 该操作将修剪sourceNSAString使其适合某个大小,然后将其返回到其他某个线程。

//sourceNSAString is a NSMutableAttributedString that will be set to nil elsewhere in another GCD queue.
CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef) sourceNSAString); 

if (frameSetter) {

    CFRange fitRange = {static_cast<CFIndex>(range.location), 0};

    CFRange totalRange = {static_cast<CFIndex>(range.location), static_cast<CFIndex>(range.length)};

    CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(frameSetter, totalRange, NULL, size, &fitRange);

    CFRelease(frameSetter);

      ...... trim sourceNSAString to fit in fitRange
 }

Is it because: 1, I should not return sourceNSAString to another thread ? 是因为:1,我不应该将sourceNSAString返回到另一个线程吗? or 2, CTFramesetterCreateWithAttributedString can't be used in background thread ? 还是2,CTFramesetterCreateWithAttributedString不能在后台线程中使用?

Any ideas? 有任何想法吗? Thanks! 谢谢!

OK, it seems that it's a simulator bug (at least for now). 好的,这似乎是模拟器错误(至少现在是这样)。 I profiled my App on a device and the memory leak just disappeared. 我在设备上分析了我的应用程序,而内存泄漏刚刚消失了。 Simulator seems to have a lot of memory leaks when using core text in multi-thread environment. 在多线程环境中使用核心文本时,模拟器似乎有很多内存泄漏。 For example, create a CTFrameSetter and release it in the same GCD serial queue later (which is allowed according to the Doc) can cause memory leak randomly. 例如,创建一个CTFrameSetter并在以后在同一GCD串行队列中释放它(根据Doc的允许)可能会随机导致内存泄漏。 Anyway all those memory leaks just go away when profiled in a real device. 无论如何,在真实设备中进行分析时,所有这些内存泄漏都会消失。

This leak is not just a simulator bug, it shows up on the actual device. 此泄漏不仅是模拟器错误,它还会显示在实际设备上。 See my answer to this related question for in depth debugging Memory usage grows with CTFontCreateWithName and CTFramesetterRef 请参阅我对这个相关问题的答案以进行深入调试, 随着CTFontCreateWithName和CTFramesetterRef的使用,内存使用量会增加

i think you should release frameSetter after if statement, because if the if statement isn't executed the frameSetter doesn't get released. 我认为您应该在if语句之后释放frameSetter,因为如果不执行if语句,frameSetter不会被释放。

    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef) sourceNSAString); 

if (frameSetter) {

    CFRange fitRange = {static_cast<CFIndex>(range.location), 0};

    CFRange totalRange = {static_cast<CFIndex>(range.location), static_cast<CFIndex>(range.length)};

    CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(frameSetter, totalRange, NULL, size, &fitRange);

 }

CFRelease(frameSetter);

this should work 这应该工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM