I am creating a Table View in viewDidAppear, because my app requires it to be created here for multiple reasons. However I notice that I get a memory leak when I analyze my application.
I thought using the instance variable _tableView is not a good idea in any other method besides init and dealloc. Should I just use autorelease, I want to make sure the table gets released at the appropriate time.
There is a property for my Table View.
@property (nonatomic, retain) UITableView *tableView;
And I create the Table View as such:
- (void)viewDidAppear:(BOOL)animated
{
self.tableView = [[UITableView alloc]
initWithFrame:CGRectMake(0, 0, 320, 300)
style:UITableViewStyleGrouped];
// Table View properties
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
}
- (void)viewDidDisappear:(BOOL)animated
{
self.tableView = nil;
}
- (void)dealloc
{
[_tableView release];
}
Autorelease. You get one retain for the alloc and another for the property assignment (assuming it’s a retained property).
Your dealloc should handle one and an autorelease the other.