bob bob - 5 months ago 37
Objective-C Question

NSString* to CFStringRef results in useless char array

I have the following code snippet:

-(CFStringRef)setupFileName:(NSString*)_name :(NSString*)_extension
NSString* tmpName = [_name stringByAppendingString:_extension];
CFStringRef ref = (__bridge CFStringRef)tmpName;
return ref;

When I break at the return statement, ref contains the right data, a nice String with extension.
But when I use it like this:

CFStringRef tickWav = [self setupFileName:_name :kTick];

It results in a useless character chain.
Is there something corrupting my encoding, when I return the right value from the function?? What can I do?


There is no automatic CFTypeRef memory management. If you were to return an NSString, it would be autoreleased. Not so for a CFStringRef. So you must do the work yourself.

You need to retain the CFStringRef with CFRetain and do a __bridge_retained cast, not a __bridge cast. You are crossing the bridge from the object world to the CFTypeRef world; you must provide memory management on this string as it crosses the bridge. You will then need to release the CFStringRef manually later on in the receiving code, or it will leak.