【问题标题】:Lot of strange leaks in instruments only on device (no leaks in simulator)仅在设备上的仪器中有很多奇怪的泄漏(模拟器中没有泄漏)
【发布时间】:2011-05-29 22:05:43
【问题描述】:

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

仪器显示我在这一行有泄漏:

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 我该如何解决?我应该去哪里找呢?

我找了一些教程,但它们都只展示了简单的例子,包括保留计数、分配、释放

【问题讨论】:

  • 为什么你需要initWithArray: 已经是自动发布的NSArray
  • 我修复了但没有解决问题

标签: iphone memory-leaks instruments


【解决方案1】:

线程不是真正的问题。它是 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];

【讨论】:

  • 我遇到了类似的问题,绝望地尝试了上述解决方案。它没有解决我的问题。我没想到会这样,因为我很确定 iOS 已经从主线程调用了 tableView:cellForRowAtIndexPath。毕竟,它是 UIKit 的一部分,UIKit 调用只能在主线程上进行。我错过了什么吗?
【解决方案2】:

为什么不直接删除initWithArray: 并使用

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

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

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

【讨论】:

  • 没有解决问题。仪器仍然显示相同的泄漏:(
  • 如果从 nib 加载,为什么要创建 UIImageView?您可以在 nib 中指定 ImageView 并将其与 ViewController 连接。然后每次创建单元格时都必须对其进行分配初始化,而不是简单地设置属性 cell.myImageView.image = [UIImage imageNamed:@"superimage.jpg"];
  • 你是对的,但它仍然不能解决问题。我仍然有泄漏:GSFontGetFamilyName、GetFontNames、GSFontGetFullName、GetContexctStack、WebThreadCurrentContext。有字体的用同一行连接,loadNib
  • 检查所有泄漏,看看是否可以绕过所有 alloc-init 语句。例如 NSString 对我来说是可疑的。如果我分配初始化一个字符串,使用它然后释放它,如果没有复制它,我将得到 EXC_BAD_ACCESS。也许您的其他泄漏来自其他任何地方,已编辑:set owner:nil
  • 没有其他与代码相关的泄漏。我找不到任何东西,可以连接。真的很奇怪
【解决方案3】:

线程造成了所有问题。我一直在做分叉而不返回主线程。这就是问题所在。

【讨论】:

  • 您能详细说明一下吗?
  • 嗯,那是很久以前的事了,我记不太清了。我认为这是与线程和 UI 相关的东西(不能在线程中修改 UI)。多线程也有问题。我在那个线程中启动了新线程,以此类推,没有返回主线程。这是一个简单而愚蠢的错误。
猜你喜欢
  • 2012-07-06
  • 1970-01-01
  • 2011-11-02
  • 1970-01-01
  • 2011-03-29
  • 2011-05-18
  • 2012-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多