2012-04-18 50 views
3

對於每次UITableview滾動時,都有48字節的內存泄漏。 責任庫:libsystem_c.dylib 責任框架:strdup。在iOS 5.1上滾動時UITableView中的內存泄露

這僅在iOS 5.1上觀察到,而不在早期版本上。 還有其他人面對過嗎?這是iOS 5.1中的錯誤嗎?

代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath(NSIndexPath *)indexPath 
{ 
    NSString *cellIdentifier = [[NSString alloc] initWithString:@"fontSelectionCell"]; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    [cellIdentifier release]; 

    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; 
    }  

    cell.textLabel.text = [fontNameList objectAtIndex:[indexPath row]]; 
    cell.selectionStyle =UITableViewCellSelectionStyleGray; 
    cell.textLabel.font = [UIFont systemFontOfSize:17.0]; 

    if ([fontName isEqualToString:cell.textLabel.text]) 
    { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     cell.textLabel.textColor = [UIColor blueColor]; 

    } 
    else 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     cell.textLabel.textColor = [UIColor blackColor]; 
    } 

    return cell; 
} 
+2

顯示一些代碼可能是? cellForRowAtIndexPath:方法將是最有趣的...您是否也在項目中使用ARC? – Vladimir 2012-04-18 08:21:34

+0

您是否使用沒有自定義代碼的默認實現?它泄漏了,還是稍後會清除內存(由於自動釋放對象)。 – calimarkus 2012-04-18 08:22:37

+0

我沒有使用ARC – 2012-04-18 08:47:23

回答

0

我認爲你有這個問題已經在iOS 5.1報告。我自己也有這個。目前,我無法在蘋果論壇上找到關於此問題的鏈接。

+0

我還遇到過幾個參考文件,指出這是iOS 5.1中的一個已知問題。一個這樣的參考文獻:http://openradar.appspot.com/11081198 – 2012-05-03 07:34:36

1

它可能是由於你的方式正在處理的小區標識。我真的很驚訝它不會讓你崩潰,因爲你釋放了cellIndentifier,但是當創建一個新的單元格時(即當一個單元格沒有從dequeueReusableCellWithIdentifier重新使用時)被引用。

標準/可接受的使用單元格標識符的方法是使用靜態的(因爲它永遠不會改變,並且它只會被分配一次而不是100次,因爲滾動時會不斷調用cellForRowAtIndexPath一張桌子)。這會讓你的代碼更高效。

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    static NSString *cellIdentifier = @"fontSelectionCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; 
    } 

    ... 
} 

你可以嘗試更改cellIdentifier,看看您是否仍然可以得到泄漏?

+0

嗨,我試過用cellIdentifier的靜態NSString,但仍然在tableView滾動時出現同樣的內存泄漏。 – 2012-04-18 09:40:12

+0

你看到哪個類/類型在泄漏嗎?儀器顯示什麼? – calimarkus 2012-04-19 11:57:50