2
    CTFontRef font = CTFontCreateWithName((__bridge  CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
    NSRange  rangeHighlight = NSMakeRange(range.location, substringToHighlight.length);
    if (font) {
        [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge  id)font range:rangeHighlight];
        CFRelease(font); //Is this still necessary?
    }

I copy and paste this code from https://github.com/mattt/TTTAttributedLabel

  CTFontRef font = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
  if (font) {
    [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)font range:boldRange];
    [mutableAttributedString addAttribute:@"TTTStrikeOutAttribute" value:[NSNumber numberWithBool:YES] range:strikeRange];
    CFRelease(font);
  }

When I do that I got an error saying that I got to use the keyword __bridge. What is it? I put it and compile error stop. But then I wonder if I still need to use CFRelease(font)

In addition

  1. What is CF in CFRelease?
  2. What is __bridge?
  3. Should I do CFRelease(font) after using __bridge?
  4. Where can I learn more about this?
user4951
  • 32,206
  • 53
  • 172
  • 282

2 Answers2

2

Yes you still need to use CFRelease(font).

You are still the one creating the font so you need to release it as well. The __bridge part is related to how the font name is retained or not.

  1. CF is short for Core Foundation, the C level API that Foundation is built upon. CFRelease is how you release a Core Foundation object.

  2. __bridge tells ARC how it should retain or not retain an objects when translating it to a Core Foundation object. This question explains the different __bridge types.

  3. You should still release (explained above).

  4. Search for "Core Foundation". The Design Concepts explains the general design.

Community
  • 1
  • 1
David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
2
  1. CF stands for "Core Foundation". A CTFontRef is a Core Foundation type so you can release it with CFRelease().

  2. __bridge is a keyword used when casting from a retainable type to a non-retainable type (or vice versa) to tell the compiler there should be no change of ownership happening.

  3. Yes, because __bridge does not change who the owner is.

  4. This documentation is fairly comprehensive.

dreamlax
  • 93,976
  • 29
  • 161
  • 209
  • I thought both CFStringRef and NSString * is retainable except that ARC automatically take care of the retain thingy? So CTFontCreateWithName increase retain count by 1 am I correct? – user4951 Jan 31 '13 at 10:44
  • CFStringRef *are* retainable per se, but not in the sense that ARC is concerned with. ARC considers a type "retainable" if it is a pointer to an Objective-C object (as opposed to a pointer to a `float` or any other kind of struct, etc). CFStringRef are not Objective-C objects so for the sake of ARC, they are non-retainable. There are functions `CFBridgingRelease` and `CFBridgingRetain` that handle the transfer of ownership. In your case, I think you can get rid of the `__bridge` **and** the `CFRelease()` if you replace the cast with `CFBridgingRelease(font)`. – dreamlax Jan 31 '13 at 22:02
  • I did CFBridgingRelease(CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL)) and it doesn't work. – user4951 Feb 01 '13 at 01:57