CGRectMake的内存泄漏和设备崩溃

时间:2013-09-19 07:16:42

标签: ios objective-c memory-management memory-leaks cgrectmake

我在我的项目中使用ARC,当我在设备中运行应用程序时它崩溃所以我检查了命令+ shift + B它显示以下泄漏.... 以下是我使用的代码,

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"ColourSelectTableCell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

            //CGRect frame = CGRectMake(15.0, 5.0, 25.0, cell.frame.size.height-10.0);
            selectedLabel = [[UILabel alloc] initWithFrame:CGRectMake(15.0, 5.0, 25.0, cell.frame.size.height-10.0)];
            selectedLabel.tag = kSelectedLabelTag;
            selectedLabel.font = [UIFont systemFontOfSize:24.0];
            selectedLabel.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.0]; // transparent
            [cell.contentView addSubview:selectedLabel];


            UIImageView *AtoZimage = [[UIImageView alloc] initWithFrame:CGRectMake(70, 10,60, 40)];
            UIImage *atozImage = [UIImage imageNamed:[bgArray objectAtIndex:indexPath.row]];
            AtoZimage.image = atozImage;
            [cell addSubview:AtoZimage];

            UILabel *lblTemp1 = [[UILabel alloc]initWithFrame:CGRectMake(150, 10, 190, 40)];
            lblTemp1.backgroundColor=[UIColor clearColor];
            lblTemp1.tag=kSelectedLabelTag;
            lblTemp1.text = [bgNameArray objectAtIndex:indexPath.row];
            [lblTemp1 setNumberOfLines:10];
            cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
            [lblTemp1 setFont:[UIFont boldSystemFontOfSize:16]];
            [cell addSubview:lblTemp1];


        }


        // Configure the cell...
        UIColor *cellColour = [self.bgArray objectAtIndex:indexPath.row];
        CMColourBlockView *colourView = (CMColourBlockView *)[cell viewWithTag:kcolourViewTag];
        colourView.colour = cellColour;

        selectedLabel = (UILabel *)[cell viewWithTag:kSelectedLabelTag];
        if ([self.selectedBg isEqual:cellColour]) {
            selectedLabel.text = @"✔";
        }
        else {
            selectedLabel.text = @"";
        }

        return cell;

    }

显示内存泄漏,如图所示 enter image description here

1 个答案:

答案 0 :(得分:0)

CGRectMake与泄漏无关;泄漏将来自分配两个UILabel和UIImageView。基于我所看到的,ARC没有为您的项目启用,因为ARC将负责在此处释放您分配的对象。

检查ARC是否已启用:

  1. 转到Project Navigator
  2. 选择Project Navigator第一行中的顶级项目项目
  3. 在“目标”下,选择您的应用
  4. 选择编辑器顶部的“构建设置”,然后搜索“自动”
  5. 找到“Objective-C自动参考计数”并将其设置为Yes