Search code examples
iphoneobjective-ciosfontscore-text

Memory usage grows with CTFontCreateWithName and CTFramesetterRef


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 !


Solution

  • this has now been fixed as long as you release the CTFramesetterRef.

    (... and make sure you reinstall your app back to your device before re-running Instruments after code changes!).