3
votes

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

3
Why do you need to initWithArray: with something that is already an autoreleased NSArray?BoltClock♦
i fixed it but didn't solve the problemwoojtekr

3 Answers

4
votes

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

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

-1
votes

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