2011-02-04 3 views
0

よろしくお願いします。しかし、深刻なことに、私のコードで漏れがどこから来ているのか分かりません。それを楽器で動かしても漏れは見えませんが、分析すると警告しています。疑わしいコードは次のとおりです。このコードのリークはどこにありますか?私は配管工と呼ぶことができますか?

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier]; 
    } 
    NSUInteger row = [indexPath row]; 

    if (cityCount >= 1) { 

     NSString *city = [NSString stringWithFormat:@"%@, %@, %@", 
          [[cityData objectAtIndex:row] objectAtIndex:0], 
          [[cityData objectAtIndex:row] objectAtIndex:1], 
          [[cityData objectAtIndex:row] objectAtIndex:2] 
          //,[[cityData objectAtIndex:row] objectAtIndex:3] 
          ]; 

     cell.textLabel.text = city; 
     cell.textLabel.font = [UIFont boldSystemFontOfSize:14]; 
     return cell; 
    } 

    else { 
     cell.textLabel.text = @"No cities were found"; 
     cell.textLabel.font = [UIFont boldSystemFontOfSize:14]; 
     cell.textLabel.textAlignment = UITextAlignmentCenter; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     return cell; //Here's the warning 
        //Potential leak of an object allocated on line 64 and stored into 'cell' 
    } 
} 

答えて

10

autoreleaseに電話する必要があります。これに

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier]; 

:これを変更

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier] autorelease]; 
+0

うん!ありがとう! –

+0

私はARCと同じ問題があります。それでも私はそれを自動リリースする必要があります。助けてください。 – UserDev

5

あなたがcellを割り当てるとき、あなたはそれをautorelease必要があります。あなたが何かを割り当てるとき、あなたは所有者であり、あなたはそれを解放しなければなりません。あなたがセルを返すので、あなたが持っている唯一のオプションはそれを自動リリースすることです。

メモリ管理ルールhereを見てください。

コードは次のようにする必要があります:

cell = [[[UITableViewCell alloc] 
    initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier] 
    autorelease]; 

今配管工のための必要はありません...それだ

+0

ありがとう、それはちょうど私がautoreleaseに滑りました。 :) –

関連する問題