2010-12-23 19 views
3

我開始使用儀器,並且有很多泄漏。我不知道如何解決它們。很多儀器上的奇怪泄漏只在設備上(模擬器中沒有泄漏)

儀器證明我有泄漏在這行:

NSArray *topLevelObjects = [[NSArray alloc] initWithArray:[[NSBundle mainBundle] loadNibNamed:@"SearchResultsTableViewCell" owner:self options:nil]]; 

有什麼不對呢?

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"SearchResultsTableViewCell"; 

SearchResultsTableViewCell *cell = (SearchResultsTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    NSArray *topLevelObjects = [[NSArray alloc] initWithArray:[[NSBundle mainBundle] 
     loadNibNamed:@"SearchResultsTableViewCell" 
     owner:self options:nil]]; 

    for (id currentObject in topLevelObjects) { 
    if ([currentObject isKindOfClass:[UITableViewCell class]]) { 
    cell = (SearchResultsTableViewCell *) currentObject; 
    break; 
    } 
    } 
    [topLevelObjects release], topLevelObjects = nil  ; 
} 

    Product *product = [productMutableArray objectAtIndex:indexPath.row]; 

cell.textLabel.text = product.title; 
cell.detailTextLabel.text = product.desc1; 
UIImageView *imageView = nil; 
if (product.photo == nil) { 
    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ph38x38.jpg"]]; 
} else { 
    imageView = [[UIImageView alloc] initWithImage:product.photo]; 
} 
imageView.frame = CGRectMake(10., 3., 38., 38.); 
[cell addSubview:imageView]; 
[imageView release]; 

    return cell; 
} 

我也有很多其他的泄漏,但儀器甚至不顯示行的代碼,例如有泄漏: GenerlaBlock-64 - UIKit中 - GetContextStack 我怎樣才能解決呢?我應該在哪裏尋找它?

我找了某種教程,但他們都表示只保留計數,分配繁瑣的例子,釋放

+0

爲什麼你需要`initWithArray:`的東西,已經是一個自動釋放`NSArray`? – BoltClock 2010-12-23 11:10:26

+0

我修好了但沒有解決問題 – woojtekr 2010-12-23 12:04:25

回答

-1

線程正在解決所有問題。我一直在分叉,而沒有返回主線程。這是問題所在。

0

你爲什麼不只是刪除initWithArray:,並使用

NSArray *topLevelObjects = [[NSBundle mainBundle] 
     loadNibNamed:@"SearchResultsTableViewCell" 
     owner:self options:nil]; 

(你必須刪除release並設置nil太行)

//編輯:嘗試設置owner:nil

+0

沒有解決問題。儀器仍然顯示相同的泄漏:( – woojtekr 2010-12-23 11:46:30

+0

)爲什麼如果從nib加載時創建UIImageView?您可以在您的nib中指定ImageView並將其與您的ViewController連接,然後您必須在每次創建單元時分配-init,你只需設置屬性cell.myImageView.image = [UIImage imageNamed:@「superimage.jpg」]; – 2010-12-23 11:50:45

4

線程不是真正的問題。這是UILabel的「文本」屬性。

cell.textLabel.text = product.title; 
cell.detailTextLabel.text = product.desc1; 

該屬性只能在主線程中設置。

呼叫performSelectorOnMainThread來代替:

[cell.textLabel performSelectorOnMainThread:@selector(setText:) withObject:product.title waitUntilDone:NO]; 
[cell.detailTextLabel performSelectorOnMainThread:@selector(setText:) withObject:product.desc1 waitUntilDone:NO];