2009-10-27 36 views
3

這是我的我的UITableViewiPhone:UITableView泄漏像泰坦尼克號!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
     static NSString *identificadorNormal = @"Normal"; 

     UITableViewCell *cell; 

     cell = [tableView dequeueReusableCellWithIdentifier:identificadorNormal]; 

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

      UILabel * myText = [[[UILabel alloc] initWithFrame:CGRectMake(5.0, 54.0, 225, 18)] autorelease]; 
      [myText setTextAlignment:UITextAlignmentLeft]; 
      [myText setBackgroundColor:[UIColor whiteColor ]]; 
      [myText setClipsToBounds:YES]; 
      [myText setFont:[UIFont systemFontOfSize:14.0]]; 
      [myText setTextColor:[UIColor blackColor]]; 
      [myText setAlpha:0.6]; 
      [myText setTag: 1]; 
      [cell addSubview:myText]; 

      UILabel * labelFRE = [[[UILabel alloc] initWithFrame:CGRectMake(235.0, 54.0, 80, 18)] autorelease]; 
      [labelFRE setTextAlignment:UITextAlignmentCenter]; 
      [labelFRE setBackgroundColor:[UIColor greenColor ]]; 
      [labelFRE setClipsToBounds:YES]; 
      [labelFRE setFont:[UIFont boldSystemFontOfSize:14.0]]; 
      [labelFRE setTextColor:[UIColor blackColor]]; 
      [labelFRE setAlpha:0.75]; 
      [labelFRE setTag: 2]; 
      [cell addSubview:labelFRE]; 
     } 

     cell.imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] 
      pathForResource:[NSString stringWithFormat: @"table%d", indexPath.row] ofType:@"jpg"]];  
     NSString * preffix = [NSString stringWithFormat: @"grat%d", indexPath.row]; 

     UILabel *myText2 = (UILabel*)[cell viewWithTag:1]; 
     [myText2 setText:NSLocalizedString(preffix, @"")]; 

     UILabel *labelFRE2 = (UILabel*)[cell viewWithTag:2]; 
     [labelFRE2 setText:NSLocalizedString(@"frKey", @"")];  
     return cell; 
} 

這是漏水就像地獄的cellForRowAtIndexPath的代碼。每當我滾動表格時,更多的泄漏被添加到樂器列表中。

你們能找到原因嗎?

感謝您的任何幫助。

編輯

第一輪徵求意見後,我已經改變了以前的代碼這個

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *identificadorNormal = @"Normal"; 

    UITableViewCell *cell; 

    cell = [tableView dequeueReusableCellWithIdentifier: identificadorNormal]; 

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

     UILabel * myText = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 54.0, 225, 18)]; 
     [myText setTextAlignment:UITextAlignmentLeft]; 
     [myText setBackgroundColor:[UIColor whiteColor ]]; 
     [myText setClipsToBounds:YES]; 
     [myText setFont:[UIFont systemFontOfSize:14.0]]; 
     [myText setTextColor:[UIColor blackColor]]; 
     [myText setAlpha:0.6]; 
     [myText setTag: 1]; 
     [cell addSubview:myText]; 
      [myText release]; 

     UILabel * labelFRE = [[UILabel alloc] initWithFrame:CGRectMake(235.0, 54.0, 80, 18)]; 
     [labelFRE setTextAlignment:UITextAlignmentCenter]; 
     [labelFRE setBackgroundColor:[UIColor greenColor ]]; 
     [labelFRE setClipsToBounds:YES]; 
     [labelFRE setFont:[UIFont boldSystemFontOfSize:14.0]]; 
     [labelFRE setTextColor:[UIColor blackColor]]; 
     [labelFRE setAlpha:0.75]; 
     [labelFRE setTag: 2]; 
     [cell addSubview:labelFRE]; 
      [labelFRE release]; 
    } 

    cell.imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] 
     pathForResource:[NSString stringWithFormat: @"table%d", indexPath.row] ofType:@"jpg"]];  
    NSString * preffix = [NSString stringWithFormat: @"grat%d", indexPath.row]; 

    UILabel *myText2 = (UILabel*)[cell viewWithTag:1]; 
    [myText2 setText:NSLocalizedString(preffix, @"")]; 

    UILabel *labelFRE2 = (UILabel*)[cell viewWithTag:2]; 
    [labelFRE2 setText:NSLocalizedString(@"frKey", @"")];  
    return cell; 
} 

泄漏繼續。根本沒有改變。

+2

它報告的泄漏對象是什麼?你有NSZombieEnabled嗎? – coneybeare 2009-10-27 23:40:17

+0

它報告「GeneralBlock-16(16字節)GraphicsServices PurplePushEvent和CreateWithEventRecord ... – SpaceDog 2009-10-27 23:49:56

+0

你能看到這些泄漏的調用堆棧嗎? – coneybeare 2009-10-28 00:10:38

回答

2

看着http://imgur.com/XPRYF這看起來不像是你的泄漏。這是在設備上還是在模擬器中?

當您使用加速度計時,曾經存在GSEvents的泄漏,但是這些應該已經在3.0中修復。

此外,請嘗試禁用NSAutoreleaseFreedObjectCheckEnabled,NSZombieEnabled和NSDebugEnabled,如果其中任何一個被啓用,因爲已經多次絆倒探查器。

我很好奇的另一件事是爲什麼你設置你的細胞取決於它們是否脫離複用隊列或不是這是一個單獨的問題。

+0

我從來沒有使用模擬器,我正在爲3.0編譯並在iPhone上看到這些泄漏 – SpaceDog 2009-10-28 02:56:13

+0

我已經註釋了LABEL行(創建和設置)並且泄漏消失了,所以問題出現在這些行中。 – SpaceDog 2009-10-28 03:04:35

+0

我發現這是兩個bug中的一個,有罪的確是加速度計,但問題是加速度計不能單獨泄漏,如果我評論標籤,加速度計不會泄漏,如果我激活標籤,加速度計會使它們泄漏,這怎麼可能?如果加速度計打開,標籤會泄漏,而我是上午COMPILING FOR 3.0在理論上,加速度計很好。 – SpaceDog 2009-10-28 05:45:39

2

我會在這裏避免autorelease,因爲在autorelease池有機會耗盡之前,您可能會多次創建對象。而且,在性能激烈的情況下(例如滾動表格)它不是那麼好。

編輯

,我要補充的情況下,有困惑,而不是在這裏使用autorelease,你反而會做

myThing = [[Obj alloc] initWithWhatever]; 
[myThing doStuff]; 
[cell addSubview:myThing]; 
[myThing release]; 
+0

好主意,當然不會傷害,但我不認爲這將是一個大問題在這裏。在第一次運行循環(當表是第一次顯示)這個方法應該只被調用15次(但屏幕上有很多行),這15個UITableViewCells將在該運行結束時被釋放l接力。稍後,在滾動時,-cellForRowAtIndexPath只會偶爾創建一個新的UITableViewCell,因爲它通常可以重用現有的單元格。 – 2009-10-28 00:33:03

+0

嗨,謝謝你的回答,但我根本看不出有什麼不同。同樣的泄漏,我正在做你所建議的。 – SpaceDog 2009-10-28 01:26:34

0

是否已確認identificadorNormal@"normal"是一樣的嗎?如果您不斷嘗試使用標識符@"normal"將某個單元格出列,但不存在,那麼您將創建一個標識符爲identificadorNormal的單元格。如果這與@"normal"不一樣,那麼每當tableview試圖顯示一個單元格時,你都會繼續創建新的單元格。

+0

謝謝。我糾正了這一點,但難以置信的是泄漏仍在繼續。 :-( – SpaceDog 2009-10-28 01:42:16

+0

我已經評論了所有在代碼上創建標籤的行,並且泄漏消失了。問題出在 – SpaceDog 2009-10-28 02:04:08

0

我不知道當UIImage加載圖像時是否會發生泄漏?

您可以嘗試使用+imageNamed代替嗎?然後UIImage會緩存圖像(它不會與+imageWithContentsOfFile:)。如果在加載圖像時發生泄漏,這應該會減少泄漏,但可能不會消除泄漏。

NSString *imageName = [NSString stringWithFormat: @"table%d.jpg", indexPath.row]; 
cell.imageView.image = [UIImage imageNamed:imageName];   
+0

我已經將LABEL行(創建和設置)註釋掉了,泄漏消失了,所以問題出現在這些行中 – SpaceDog 2009-10-28 03:05:18

0

我在自己的項目中複製了您的代碼,並且它在模擬器中泄漏,但是在我擁有的任何設備(iPhone 3GS,iPod Touch 2G)上都沒有泄漏。

假設你也在模擬器中看到麻煩,我想這是蘋果建議你測試真實設備的另一個確認。

+0

我從來沒有使用模擬器。我正在編譯3.0,並在iPhone上看到這些泄漏。 – SpaceDog 2009-10-28 02:55:20

+0

我已將LABEL行(創建和設置)註釋掉,並且泄漏消失。所以問題出在這些方面。 – SpaceDog 2009-10-28 03:03:56

+0

奇怪。我之前看到你的評論只是關於造成麻煩的標籤行,而只是複製了那些標籤行。在運行3.2.1的3GS和iPhone 2G上我看不到什麼麻煩......非常,非常奇怪。 – refulgentis 2009-10-28 03:39:44

0

從未在此發佈過,也不確定我是否正確地跟蹤該主題。我的建議是檢查你正在使用的編譯器優化級別。對於某些項目(我可以稍後詳細說明,如果這種方法有幫助),像這樣的奇怪問題可以通過使用-o或-o1消失。任何更高的值,似乎實例變量的初始化值不總是可信的。

+0

請詳細說明您的解釋... – SpaceDog 2009-11-13 20:20:58

相關問題