0

Update

Solved the issue and there was an cycle retain.

Original Question

Profile showed zero memory leak, however the app used more and more memory as time went.

Let's just take one of the things in the app, and have a detailed look. There is a table view - let's call it the main table, when click on any of its cell, it leads you to a second table view and the second one has 10 to 20 images on it.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FlyerListTable *detail = [[FlyerListTable alloc] initWithNibName:@"FlyerListTable" bundle:nil]; 
    detail.department = [categories objectAtIndex: indexPath.row];
    detail.promotions = [items valueForKey:detail.department];
    [self.navigationController pushViewController:detail animated:NO];
    [detail release];
}

FlyerListTable is the class for the second table, and it has dealloc defined, however I traced it and this dealloc method was never called.

勿绮语
  • 9,170
  • 1
  • 29
  • 37

1 Answers1

1

What's the convention or best practice?

I would suggest you to make it lazy loaded:

- (FlyerListTable*)flyerListTable
{
    if (_flyerListTable == nil)
        _flyerListTable = [[FlyerListTable alloc] initWithNibName:@"FlyerListTable" bundle:nil];
    return _flyerListTable;
}

Do not forget to release it in dealloc.

Then use it when the row is selected

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FlyerListTable* detail = [self flyerListTable];
    detail.department = [categories objectAtIndex: indexPath.row];
    detail.promotions = [items valueForKey:detail.department];
    [self.navigationController pushViewController:detail animated:NO];
}

When I traced it, this dealloc method was never called.

If dealloc is not called in your example it means some other object has retained it. Try to find out what object may retain it before you make any changes. You can override retain method for this purpose:

- (id)retain
{
    return [super retain];
}

Set a break point to see the call stack. You should not use ARC of course.

Good luck!

Davyd Geyl
  • 4,578
  • 1
  • 28
  • 35
  • @PeterPeiGuo You are right, the navigation controller should release it after it is popped. – Davyd Geyl Oct 26 '11 at 23:34
  • @vikingosegundo I am not encouraging anyone to use retainCount at all :) – Davyd Geyl Oct 26 '11 at 23:37
  • I posted the link as response to @PeterPeiGuo's comment. – vikingosegundo Oct 26 '11 at 23:55
  • Lazy loaded object does not solve the retaining issue, you need to solve it regardless. You can allocate a new object every time you select a new row if the performance is to an issue. I would prefer to reuse already allocated FlyerListTable and release it if there is low memory warning. – Davyd Geyl Oct 27 '11 at 05:56