I'm having some interesting issues with CoreText on iPhone, causing memory leaks in certain circumstances.
I've looked everywhere in the documentation and across the internet and no-one seems to be getting the same issue. However, the circumstances are perhaps special (see below).
Anyway, after a lot of narrowing down, I managed to get this repro case:
void leakTest(NSString* fontname, NSString* text)
{
NSDictionary* descriptorAttr = [NSDictionary dictionaryWithObjectsAndKeys:
fontname, (const NSString*)kCTFontFamilyNameAttribute, nil];
CTFontDescriptorRef descriptor = CTFontDescriptorCreateWithAttributes((CFDictionaryRef)descriptorAttr);
CTFontRef font = CTFontCreateWithFontDescriptor(descriptor, 0, nil);
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
(id)font, (const NSString*)kCTFontAttributeName, nil];
CFRelease(descriptor);
CFRelease(font);
NSMutableAttributedString* string = [[NSMutableAttributedString alloc] initWithString:text];
[string setAttributes:dict range:NSMakeRange(0, string.length)];
CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedString((CFMutableAttributedStringRef)string);
CFRelease(typesetter);
[string release];
}
Depending on the font and text, this may or may not leak according to the Leaks tool - various/numerous things but definitely seeming font and font descriptor related.
These can be caused as follows:
const char* unicodeText = "Ernle\310\235e"; // "Ernleȝe" if your editor groks unicode
NSString* textLeaky = [NSString stringWithUTF8String:unicodeText];
NSString* textNormal = @"Hello World";
leakTest(@"Courier", textNormal); // This doesn't leak
leakTest(@"Courier", textLeaky); // This does leak
leakTest(@"Arial", textLeaky); // This doesn't leak with this font?
Obviously I commented out so as to leave one leakTest call to test with the Leaks tool!
So, the string with an unusual but perfectly legal unicode character in causes a leak with one font but not the other.
Is there something wrong in my code and I happen to be "getting away with it" with Arial? I wondered whether there may be some kind of font caching that was causing the apparent leak but the OS or whatever is actually all handling it okay.
Hope you can help folks!