UITableView leaking?

2019-09-06 13:42发布

问题:

I have a UITableViewController and I set some properties in the viewDidLoadMethod like so:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self.parentViewController.parentViewController action:@selector(dismissSettings)] autorelease];
    [self setTitle:@"Gabbai Settings"];

    //Set up the table 
    [self.tableView initWithFrame:self.tableView.frame style:UITableViewStyleGrouped];

    [self.tableView setRowHeight:65.0];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLineEtched];

    [self.tableView.backgroundView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@_settings", kTheme] ofType:@"png"]]]];


}

The app shows a UIScrollViewController and a UIButton. When the button is pressed, the app displays a modal UINavigationController and that UINavigationController contains a UITableViewController.

For some reason, Leaks reports some leakage when I show the UITableViewController. However, if I comment out the four lines after //Set up the table, everything is fine.

When the four lines are uncommented, the Leaks instrument shows the following:

I'm not sure what is going on here, but it's really annoying. I've used the same four lines of code before to customize my UITableViewController and this is a new behavior.

What could be wrong?

回答1:

As has been pointed out by other answers, your problem is the line:

[self.tableView initWithFrame:self.tableView.frame style:UITableViewStyleGrouped];

It will create a new UITableView object and it will not modify self.tableView which means that line is totally pointless. It only creates the leak you are experiencing. Remove it and you should be fine.



回答2:

Explicitly release tableView in your dealloc method and see what happens.

Or better yet, clean all -> Build and Analyze, tell us what you see



回答3:

if self.tableview already exists, you don't want to initialize it again. Either initialize it once (which I'm guessing you already did, maybe by loading from a nib) and then if need be, reset the frame here.

Or just alloc/init the tableview here.



回答4:

Maybe you can try defining the uiBarButtonItem as a variable, then assign it to the navigation left bar button item and then release the button

Have you try build & analyze under the Build menu? With 2.0 Objective C sentences its pretty accurate

in addition, you have to be careful with property definition, you are using self.tableView, I assume you you have something like @property(nonatomic, retain)?, in that case, once you alloc, you should release the property in the next line..

Finally, this line is extremely weird: [self.tableView initWithFrame:self.tableView.frame style:UITableViewStyleGrouped]; you are trying to initialize a table thats already initialized, because if you don't, self.tableView.frame should be in error. In addition to that, the initWithFrame method returns something that is not being assigned.. I think that you can think a better way to do this

Sorry about the mess, I've been answering this question in the chat, but unfortunately you were disconnected.

Hope this helps.



回答5:

Looks like you have an array or dictionary thats leaking, not the tableview.

What is the array that you are using to fill your tableview?

Also, you wont want to [self.tableVIew initWithFrame, try [self.tableView setFrameCGRectMake(x,y,w,h)];