Reputation: 19507
I am converting my project to ARC and Xcode thinks there is a memory leak here, does anyone see anything wrong with this? I didn't write this code so I am not familiar with C calls.
- (NSString*) URLEscaped
{
NSString *encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)self,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
return encodedString;
}
Upvotes: 0
Views: 1431
Reputation: 288
Under ARC you can use __bridge_transfer to transfer memory management of the returned string to ARC:
NSString *encodedString = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)self,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
Upvotes: 4
Reputation: 1904
From the docs:
There are many ways in which you can get a reference to an object using Core Foundation. In line with the Core Foundation ownership policy, you need to know whether or not you own an object returned by a function so that you know what action to take with respect to memory management. Core Foundation has established a naming convention for its functions that allows you to determine whether or not you own an object returned by a function. In brief, if a function name contains the word "Create" or "Copy", you own the object. If a function name contains the word "Get", you do not own the object.
Yes, you're leaking a CFString
there.
Upvotes: 1