简体   繁体   中英

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)
  • //[myTableView 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)

您的方法不retain myTableView ,因此不应该release它。

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, ie, you only have to release the table view once in your dealloc method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM