2

我有一个运行良好的应用程序。但我需要没有内存泄漏。所以我决定用分析器构建,然后我看到了很多小的内存管理问题。我整理了除了这个之外的几乎所有东西。这里我的单元格对象是显示为泄漏的对象,但实际上我不知道我在哪里走错了。这是我的cellforRowatIndexpath方法。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:EventCellIdentifier];


    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:EventCellIdentifier];

        UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cell_with_arrow.png"]];
        [cell setBackgroundView:img];
        [img release];    

        cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:13.0];
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; // Pre-iOS6 use UILineBreakModeWordWrap
        cell.textLabel.numberOfLines =0; 
        [cell.textLabel sizeToFit];


    }


     [[cell viewWithTag:12] removeFromSuperview];
    [[cell viewWithTag:13] removeFromSuperview];

    PTPusherEvent *event = [eventsReceived objectAtIndex:indexPath.row];







    //cell.textLabel.text = event.name;
    // cell.detailTextLabel.text = [event.data description];

    NSDictionary *payload =event.data;
    NSLog(@"%@",payload);
    NSString *tes=[payload objectForKey:@"from"];
    NSString *subtitle = [NSString stringWithString: @"From "];
    subtitle = [subtitle stringByAppendingFormat:@"%@",tes];










    cell.detailTextLabel.text = subtitle;
    cell.textLabel.text = [payload objectForKey:@"message"];

    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor]; 



    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    // [dateFormat setDateFormat:@"dd-MMM-YYYY HH:mm:ss"];
    [dateFormat setDateFormat:@"dd-MMM-YYYY hh:mm a"];
    [dateFormat setTimeZone:[NSTimeZone defaultTimeZone]]; 
    NSDate *todaysdate=[NSDate date];
    NSString *todaysdateString = [dateFormat stringFromDate:todaysdate];
    [dateFormat release];

    CGSize boundingSize = CGSizeMake(320, CGFLOAT_MAX);
    CGSize requiredSize = [cell.textLabel.text sizeWithFont:cell.textLabel.font
                                          constrainedToSize:boundingSize
                                              lineBreakMode:UILineBreakModeWordWrap];
    CGFloat requiredHeight = requiredSize.height;  


    MarqueeLabel *rateLabelOne =  [[MarqueeLabel alloc] initWithFrame:CGRectMake(115, requiredHeight+15,155, 20) rate:50.0f andFadeLength:10.0f];    
    rateLabelOne.numberOfLines = 1;
    rateLabelOne.marqueeType = MLContinuous;
    rateLabelOne.opaque = NO;
    rateLabelOne.enabled = YES;
    rateLabelOne.shadowOffset = CGSizeMake(0.0, -1.0);
    rateLabelOne.textAlignment = UITextAlignmentRight;
    rateLabelOne.textColor = [UIColor redColor];
    rateLabelOne.backgroundColor = [UIColor clearColor];
    rateLabelOne.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.000];
    rateLabelOne.text = @"OFFline!cannot communicate now.....";

    MarqueeLabel *rateLabelTwo = [[MarqueeLabel alloc] initWithFrame:CGRectMake(215, requiredHeight+15,100, 20) rate:50.0f andFadeLength:10.0f];
    rateLabelTwo.marqueeType = MLContinuous;
    rateLabelTwo.numberOfLines = 1;
    rateLabelTwo.opaque = NO;
    rateLabelTwo.enabled = YES;
    rateLabelTwo.shadowOffset = CGSizeMake(0.0, -1.0);
    rateLabelTwo.textAlignment = UITextAlignmentRight;
    rateLabelTwo.textColor = [UIColor blueColor];
    rateLabelTwo.backgroundColor = [UIColor clearColor];
    rateLabelTwo.font = [UIFont fontWithName:@"Helvetica" size:12.000];
    rateLabelTwo.text = todaysdateString;

    if([chatstatus isEqualToString:@"online"])
    {

        rateLabelTwo.tag=12;

        [cell addSubview:rateLabelTwo];
        [rateLabelTwo release];



    }
    else
    {
        rateLabelOne.tag=13;

        [cell addSubview:rateLabelOne];
        [rateLabelOne release];



    }


return cell;
}

谁能帮我找出那个怪物在哪里?

4

4 回答 4

3

你需要把一个autorelease作为,

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:EventCellIdentifier] autorelease];

否则会被泄露。

更新:

正如 AppleDelegate 所说,

[rateLabelTwo release];并且[rateLabelOne release];还需要移出 if 条件,以便在两种情况下都会释放ifelse。分析仪也会将此检测为泄漏。

于 2012-10-23T06:37:39.973 回答
2

好的,这就是问题所在..

 if([chatstatus isEqualToString:@"online"])
    {

        rateLabelTwo.tag=12;

        [cell addSubview:rateLabelTwo];
        [rateLabelTwo release];



    }
    else
    {
        rateLabelOne.tag=13;

        [cell addSubview:rateLabelOne];
        [rateLabelOne release];



    }

在您的代码中,rateLabelOneandrateLabelTow在循环中被释放。if静态分析器将其分析为泄漏,因为它假设可能存在代码不在循环内运行的情况if。实际上它不是泄漏。

于 2012-10-23T06:54:50.360 回答
0

使用这一行:

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:EventCellIdentifier]autorelease];

这将消除您的内存泄漏。

于 2012-10-23T06:38:00.500 回答
0
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:EventCellIdentifier]autoRelease];
于 2012-10-23T06:56:34.843 回答