0

Possible Duplicate:
Normal casting vs bridge casting in Objective - C

With ARC, Xcode gently guides me to bridge cast my string to a CFString like this

MIDISourceCreate(theMidiClient, (__bridge CFStringRef)outName,
                 &midiOut);
  1. What does this bridge mean?
  2. Are there any consequences for memory management of doing this cast? Will the cast object be, um, cleaned up when it goes out of scope?
Community
  • 1
  • 1
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
  • @JoshCaswell if you think it's really a dupe I'll delete, but I think my question is different. I'm not asking if it's due to ARC. – Dan Rosenstark Jun 26 '12 at 19:51
  • Not to nitpick, but CFStrings are not C strings, if by "C string" you mean a null-terminated array of chars. – Monolo Jun 26 '12 at 19:52
  • @Monolo that's not a nitpick, but either fix it or tell me what to change it to, please. – Dan Rosenstark Jun 26 '12 at 19:52
  • 1
    I think that the answer there probably answers your question. I don't think you should necessarily remove your post. – jscs Jun 26 '12 at 19:53
  • 1
    @Yar you could just call it a `CFString`. The reason for my comment was that C strings, typically held in malloc'ed memory can't be passed to control under ARC with a simple cast - to my knowledge, at least. – Monolo Jun 26 '12 at 19:56
  • 2
    Yeah, the link there explains it. See [Transitioning to ARC Release Notes](http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html), section Managing Toll-Free Bridging. __bridge says ownership is not transferred, so the CFStringRef will not have extra CFRelease called on it. – Piotr Kalinowski Jun 26 '12 at 20:36
  • @PiotrKalinowski so this one line, as is, would cause a leak, right? – Dan Rosenstark Jun 26 '12 at 21:42
  • 1
    Nope. ARC will release outName somewhere. But if you want to 'retain' the converted pointer, you need to use __bridge_retained, otherwise ARC will insert release for outName, and when you later try to use the converted pointer, you'll get a reference to already freed memory. – Piotr Kalinowski Jun 27 '12 at 07:23
  • @PiotrKalinowski thanks. That was my question, and it was not answered directly in the other question. Should this question get reoepened, you should make an answer with that. – Dan Rosenstark Jun 27 '12 at 13:07

0 Answers0