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
Why don't you just remove the
initWithArray:
and use(and you have to remove the
release
and setnil
line too)// EDIT: try to set the
owner:nil
Threads aren't the real problem. It's the "text" property of UILabel.
This property can only be set on the main thread.
Call performSelectorOnMainThread instead:
Threads were making all the problems. I was doing forks all the time without returning to main thread. That was the problem.