无法在我的UITableViewController viewDidLoad方法中查明UIView内存泄漏

时间:2012-04-01 12:30:45

标签: ios cocoa-touch

我一直在清理我的代码,到目前为止一直很好,我只留下一个泄漏来照顾;我找不到的那个!

Instruments指出我的UITableViewController中的以下代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    self.parentView.backgroundColor = [UIColor clearColor];
    self.parentView.opaque = NO;

    self.mainButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.mainButton setTitleColor:[UIColor colorWithRed:0.16f green:0.16f blue:0.16f alpha:1.0] forState:UIControlStateNormal];
    [self.mainButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.mainButton.titleLabel.shadowOffset = CGSizeMake(0.0, 1.0);
    [self.mainButton setBackgroundImage:[UIImage imageNamed:@"grayButton.png"] forState:UIControlStateNormal];
    self.mainButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
    self.mainButton.frame = CGRectMake(0, 4, 102, 35);
    self.mainButton.center = CGPointMake(160, 25);
    [self.mainButton setTitle:@"Control" forState:UIControlStateNormal];
    [self.mainButton addTarget:self action:@selector(controlIt) forControlEvents:UIControlEventTouchUpInside];

    [self.parentView addSubview:self.mainButton];
    self.tableView.tableHeaderView = self.parentView;
}

parentView已在dealloc中发布。我也发布了mainButton,但我不确定在这种情况下是否需要它。

我想知道它是否与这一行有关:

self.tableView.tableHeaderView = self.parentView;

奇怪的是,泄漏指向一个我认为是视图控制器主视图的地址,因为它在历史记录中有超过75个内存保留/释放/自动释放条目。

真的坚持这一点,会很感激一些指导。

2 个答案:

答案 0 :(得分:1)

如果parentView是保留属性,则会泄漏:

self.parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];

您在dealloc中的发布会平衡属性赋值,但没有任何平衡分配。 尝试产品 - >分析并查看它的内容。

答案 1 :(得分:1)

你指出的那条线是问题所在。通过将parentView设置为另一个属性,它仍将具有+1的保留计数和泄漏。在-dealloc中将该属性设置为nil。