勿绮语
勿绮语

Reputation: 9320

iphone app uses more and more memory

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.

Upvotes: 0

Views: 99

Answers (1)

Davyd Geyl
Davyd Geyl

Reputation: 4623

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!

Upvotes: 1

Related Questions