problem of memory leaks in iphone app
I am getting leaks on the following code.
cell.lblNoOfReplay.text=[NSString stringWithFormat:@"0 Replies. %@",(NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)[[NSString stringWithFormat:@"Last message on %@",[BabbleVilleAppDelegate dateByAddingHours:Babbleoffset withDate:[[arrayPMMainList objectAtIndex:[indexPath section]] objectForKey:@"datetime"]]] stringByRepla开发者_如何学GocingOccurrencesOfString:@"+" withString:@" "], CFSTR(""), kCFStringEncodingUTF8)];
Here i dont havent allocated any string but when i check for memory leaks there are some leaks in the above line . Probably it may be due to kCFAllocatorDefault ,so of someome has come across the same issues , help me out .
Regards Mrugen
Yes, you have allocated a string. Core Foundation objects follow the Create rule: any object obtained via a function whose name contains either Create or Copy is owned by the caller and must be released by the caller when it’s finished using it.
Change your code to:
CFStringRef s = CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)[[NSString stringWithFormat:@"Last message on %@",[BabbleVilleAppDelegate dateByAddingHours:Babbleoffset withDate:[[arrayPMMainList objectAtIndex:[indexPath section]] objectForKey:@"datetime"]]] stringByReplacingOccurrencesOfString:@"+" withString:@" "], CFSTR(""), kCFStringEncodingUTF8);
cell.lblNoOfReplay.text=[NSString stringWithFormat:@"0 Replies. %@", (NSString *)s];
CFRelease(s);
Also, consider breaking that line into multiple pieces and intermediate variables. Other people that see that code, including your future self, will thank you.
精彩评论