2011-04-23 38 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); 

此外,考慮打破這一行成多個部分,中間變量。看到這些代碼的其他人,包括你未來的自己,都會感謝你。