2011-04-23 57 views
0

我在下面的代码泄漏。iPhone应用程序内存泄漏问题

  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"]]] stringByReplacingOccurrencesOfString:@"+" withString:@" "], CFSTR(""), kCFStringEncodingUTF8)]; 

在这里,我不还没有分配任何字符串,但是当我检查内存泄漏有在上述行一些泄漏。 可能是因为kCFAllocatorDefault,所以有人遇到了同样的问题,帮我解决。

问候 Mrugen

+2

有那么多关于这个怪物的代码行,我建议你用一些临时变量来分解它。这也将帮助你缩小泄漏。 – 2011-04-23 05:41:00

回答

5

是的,你分配一个字符串。核心基础对象遵循Create rule:通过名称包含Create或Copy的函数获得的任何对象都由调用者拥有,并且必须在调用者完成使用时释放。

你的代码更改为:

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); 

此外,考虑打破这一行成多个部分,中间变量。看到这些代码的其他人,包括你未来的自己,都会感谢你。