我有一个工作正常的应用程序。但我需要内存泄漏免费。所以我决定使用分析器构建,然后我看到了很多小的内存管理问题。我除了这个以外几乎所有的东西都整理好了。单元格对象显示为泄漏的对象,但实际上我不知道我错在哪里。这是我的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;
}
有人能帮助我找出那个怪物在哪里吗?
答案 0 :(得分:3)
您需要将autorelease
设为
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:EventCellIdentifier] autorelease];
否则会泄露。
<强>更新强>
如AppleDelegate所述,
[rateLabelTwo release];
和[rateLabelOne release];
也需要移出if条件,以便在if
和else
个案例中释放。分析仪也会将此检测为泄漏。
答案 1 :(得分:2)
好的,这就是问题所在......
if([chatstatus isEqualToString:@"online"])
{
rateLabelTwo.tag=12;
[cell addSubview:rateLabelTwo];
[rateLabelTwo release];
}
else
{
rateLabelOne.tag=13;
[cell addSubview:rateLabelOne];
[rateLabelOne release];
}
在您的代码中,rateLabelOne
和rateLabelTow
在if
循环中释放。静态分析器将其分析为泄漏,因为它假定可能存在代码无法运行的情况在if
循环内部。实际上它不是泄漏。
答案 2 :(得分:0)
使用此行:
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:EventCellIdentifier]autorelease];
这将消除您的内存泄漏。
答案 3 :(得分:0)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:EventCellIdentifier]autoRelease];