简体   繁体   中英

iPhone memory leaking?

Really quick question that is driving me INSANE. I was wondering if someone 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM