方法返回具有+1保留计数的Objective-C对象

Phi*_*lip 1 memory-leaks objective-c

当我分析我的代码时,我会开始这些:

Method returns an Objective-C object with a +1 retain count
Run Code Online (Sandbox Code Playgroud)

Object leaked: object allocated and stored into 'headerLabel' is not referenced later in this execution path and has a retain count of +1
Run Code Online (Sandbox Code Playgroud)

在这个方法上:

 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        // create the parent view that will hold header Label
        UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(15.0, 0.0, 300.0, 44.0)];

        // create the button object
        UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        headerLabel.backgroundColor = [UIColor clearColor];
        headerLabel.opaque = NO;
        headerLabel.textColor = [UIColor whiteColor];
        headerLabel.highlightedTextColor = [UIColor whiteColor];
        headerLabel.font = [UIFont boldSystemFontOfSize:15];
        headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 44.0);

        if (section == 0)
            headerLabel.text = NSLocalizedString(@"A", @"A");
        else if (section == 1) 
            headerLabel.text =NSLocalizedString(@"B", @"B");
        else if (section == 2)
            headerLabel.text = NSLocalizedString(@"C", @"C");

        if(searching)
            headerLabel.text = NSLocalizedString(@"SEARCH", @"Search Results");

        [customView addSubview:headerLabel];

        return customView;
    }
Run Code Online (Sandbox Code Playgroud)

现在,扩展我想要理解的箭头,并且我认为 customView没有被释放.这样对吗?

我该怎么做才能修复它?我是新来的,帮助我理解!

Eug*_*ene 5

要么添加

[headerLabel release];
Run Code Online (Sandbox Code Playgroud)

[customView addSubview:headerLabel];
Run Code Online (Sandbox Code Playgroud)

或者像这样初始化它

UILabel * headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
Run Code Online (Sandbox Code Playgroud)

当然,鉴于你没有使用ARC