Memory management, release needed?
I wonder if someone could quickly help me with the following, do I need to add a [myTableView release]; after I call [view addSubview:[self myTableView]]; ? Initially I was thinking no, and running it through CLANG produced to memory warnings
Here is my thinking:
- [self setMyTableView:tempTableView]; retainCount = (+1)
- [view addSubview:[self myTableView]]; retainCount = (+2)
- //[my开发者_如何学编程TableView release]; << HERE retainCount = (+1)
- -dealloc [myTableView release]; retainCount = ( 0)
.
@property (nonatomic, retain) UITableView *myTableView;
.
- (void)loadView {
NSLog(@"%s", __PRETTY_FUNCTION__);
[self setTitle:@"Location Data"];
CGRect viewFrame = CGRectMake(0, 20, 320, 460);
UIView *view = [[UIView alloc] initWithFrame:viewFrame];
CGRect tableFrame = CGRectMake(0, 0, 320, 416);
UITableView *tempTableView = [[UITableView alloc] initWithFrame:tableFrame];
[self setMyTableView:tempTableView];
[tempTableView release];
[view addSubview:[self myTableView]];
//[myTableView release]; << HERE
[[self myTableView] setDelegate:self];
[[self myTableView] setDataSource:self];
[self setView:view];
[view release];
}
.
- (void)dealloc {
[myTableView release];
[dataModel release];
[super dealloc];
}
EDIT: hmm maybe I don't as [view addSubview:[self myTableView]]; retains it and will release when done. Your right Carl, my bad. I was getting confused with: alloc, set, release, when this is simply the view taking ownership (and the responsibility to release that later)
You're right when you say that [view addSubview:[self myTableView]];
will retain your table view, but since it's the view
who is retaining it, it's the view
that is supposed to release it. And it will, when the view gets dealloc'ed. You only have to release what you retained yourself, i.e., you only have to release the table view once in your dealloc method.
Your method doesn't retain
myTableView
, so it shouldn't release
it.
精彩评论