- (NSString*)encodeURL:(NSString *)string
{
NSString *newString = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
if (newString)
{
return newString; // <-- potential leak here
}
return @"";
}
I'm not familiar with CFTypes (other than knowing what they are). This is code I got from the internet and have had to finagle a bit to get it to work in ARC. I'm getting a potential leak warning and I'm not sure how to fix it. Suggestions?
Yes, this is a memory leak. You meant to use
CFBridgingRelease()
rather than__bridge
.The object created by
CFURLCreateStringByAddingPercentEscapes
has an extra retain on it because it includesCreate
. You need to transfer that object to ARC, letting it know to add an extra release, that's whatCFBridgingRelease()
does.You do use
__bridge
for the passed-in string because you're not transferring it to Core Foundation. You're just asking Core Foundation to use it while ARC continues to own it. When you "transfer" ownership, you generally mean "this object used to be Core Foundation, and now it's ARC" (or vice versa). That's what's happening tonewString
.I swapped the long NS-to-CF encoding function with the result just to make it shorter.