I'm currently looking at a NSMutableSet
created by the function CFSetCreateMutable()
. The documentation states that the return value of CFSetCreateMutable()
is toll-free bridged, meaning that I can simply cast it into a NSMutableSet
. Does this mean that sending it the release
message is perfectly valid? Am I always safe to assume that I can always treat such objects as if they were alloc
'ed via an NS-class?

- 50,943
- 13
- 104
- 142

- 4,839
- 3
- 28
- 51
2 Answers
Just imagine that CFSetCreateMutable()
is equivalent to [[NSMutableSet alloc] init] in that you have to release the object after you are done with it. If you are using ARC, you can cast a CFMutableSet to an NSMutableSet using a bridged cast: (__bridge_transfer NSMutableSet *)theCFSet
. This will tell ARC that it is responsible for releasing the set after it goes out of scope.

- 15,557
- 5
- 43
- 45

- 7,512
- 4
- 32
- 30
-
So basically, toll-free bridge means "equivalent in all intents and purposes"? Nice! Sadly, I cannot use ARC, because I need to deploy to iOS 4. Oh well, if get to do grunt work on memory management, I will at least get a deeper understanding of it all (that's not to say I really miss RAII from C++!) – Jörgen Sigvardsson Dec 31 '11 at 21:46
The documentation states that the return value of CFSetCreateMutable() is toll-free bridged, meaning that I can simply cast it into a NSMutableSet. Does this mean that sending it the release message is perfectly valid?
Yes. This is explicitly guaranteed in the docs:
Note from the example that the memory management functions and methods are also interchangeable—you can use
CFRelease
with a Cocoa object andrelease
andautorelease
with a Core Foundation object.
Am I always safe to assume that I can always treat such objects as if they were alloc'ed via an NS-class?
Not really: Currently, anything that exists in common between CFType and NSObject, such as -description
/CFCopyDescription
, works regardless of how you created the object, but they're not all explicitly guaranteed to work like the memory-management messages are.

- 95,783
- 15
- 211
- 370