Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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??

like image 945
gabaum10 Avatar asked Dec 08 '22 00:12

gabaum10


1 Answers

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);
like image 124
James Huddleston Avatar answered Dec 09 '22 12:12

James Huddleston