Lot of strange leaks in instruments only on device (no leaks in simulator)

巧了我就是萌 提交于 2019-12-22 09:49:27

问题


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


回答1:


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



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/4518105/lot-of-strange-leaks-in-instruments-only-on-device-no-leaks-in-simulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!