仅在设备上的仪器中有很多奇怪的泄漏(模拟器中没有泄漏)

发布于 2024-10-08 15:42:00 字数 1727 浏览 4 评论 0原文

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

仪器显示我的这条线有泄漏:

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;
}

我还有很多其他泄漏,但 Instruments 甚至没有在代码中显示行,例如有一个泄漏: GenerlaBlock-64 - UIKit - GetContextStack 我该如何解决?我应该去哪里寻找它?

我寻找了某种教程,但所有这些都只显示了保留计数、分配、释放的简单示例

I started working with instruments and have a lot of leaks. I don't have an idea how to solve them.

Instrument show that i have leak in this line:

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

What is wrong with this?

// 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;
}

I also have a lot of other leaks, but instruments doesn't even show line in the code, for example there is a leak:
GenerlaBlock-64 - UIKit - GetContextStack
How can I solve it? where should i look for it?

I looked for some kind of tutorials, but all of them show only trival examples with retain count, alloc, release

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

开始看清了 2024-10-15 15:42:00

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

Threads aren't the real problem. It's the "text" property of UILabel.

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

This property can only be set on the main thread.

Call performSelectorOnMainThread instead:

[cell.textLabel performSelectorOnMainThread:@selector(setText:) withObject:product.title waitUntilDone:NO];
[cell.detailTextLabel performSelectorOnMainThread:@selector(setText:) withObject:product.desc1 waitUntilDone:NO];
樱娆 2024-10-15 15:42:00

为什么不删除 initWithArray: 并使用

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

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

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

Why don't you just remove the initWithArray: and use

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

(and you have to remove the release and set nil line too)

// EDIT: try to set the owner:nil

倾城泪 2024-10-15 15:42:00

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

Threads were making all the problems. I was doing forks all the time without returning to main thread. That was the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文