title: Memory leak when using Unicode text with certain fonts with CoreText on iPhone
body: <p>I'm having some interesting issues with CoreText on iPhone, causing memory leaks in certain circumstances. </p>
  
  <p>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).</p>
  
  <p>Anyway, after a lot of narrowing down, I managed to get this repro case:</p>
  
  <pre><code>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];
  }
  </code></pre>
  
  <p>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. </p>
  
  <p>These can be caused as follows:</p>
  
  <pre><code>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?
  </code></pre>
  
  <p>Obviously I commented out so as to leave one leakTest call to test with the Leaks tool!</p>
  
  <p>So, the string with an unusual but perfectly legal unicode character in causes a leak with one font but not the other.</p>
  
  <p>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.</p>
  
  <p>Hope you can help folks!</p>