仪器上只有很多奇怪的泄漏(模拟器没有泄漏)

时间:2010-12-23 10:57:17

标签: iphone memory-leaks instruments

我开始使用乐器并且有很多泄漏。我不知道如何解决它们。

仪器显示我在此行中有泄漏:

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 我该如何解决?我应该在哪里寻找它?

我找了一些教程,但是所有教程都只展示了保留计数,分配,发布的三维示例

3 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

为什么不删除initWithArray:并使用

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

(您必须删除release并设置nil行)

//编辑:尝试设置owner:nil

答案 2 :(得分:-1)

线程正在解决所有问题。我一直在做叉子而不返回主线程。这就是问题所在。