Really quick question that is driving me INSANE. I was wondering if s开发者_Go百科omeone could tell me why this line is leaking?
NSString *post = [NSString stringWithFormat:@"<someXML><tagWithVar=%@></tagWithVar></someXML>",var];
post = [NSString stringWithFormat:@"xmlValue=%@",(NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)post,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8 )];
I am just encoding a string into a URL format. From my understanding, stringWithFormat: should return an autoreleased object. Apparently that is not the case. It works, but leaks. Any ideas??
You are using the method CFURLCreateStringByAddingPercentEscapes
. If a Core Foundation function has "Create" in its name, it means that you own the returned object. In other words, you'll need to release the CFStringRef
returned by CFURLCreateStringByAddingPercentEscapes
.
NSString *post = [NSString stringWithFormat:@"...", var];
CFStringRef stringRef = CFURLCreateStringByAddingPercentEscapes(...);
post = [NSString stringWithFormat:@"xmlValue=%@",(NSString *)stringRef];
CFRelease(stringRef);
精彩评论