【发布时间】:2011-11-01 09:48:39
【问题描述】:
我对检查对象的保留计数有一点疑问:
请看下面的代码,释放对象内存后显示retainCount为1。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGRect contentRect = [cell.contentView bounds];
UIImageView *thumbView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:IMAGE_CELL_BACKGROUND]];
thumbView.frame = contentRect;
cell.backgroundView=thumbView;
[thumbView release];
}
UIImageView *image=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"left_arrow.png"]];
**NSLog(@"Image retain count %d",[image retainCount]);**
image.frame=CGRectMake(290, 12.5, [UIImage imageNamed:@"left_arrow.png"].size.width, [UIImage imageNamed:@"left_arrow.png"].size.height);
image.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:image];
[image release];
**NSLog(@"Image retain count-- after %d",[image retainCount]);**
// Configure the cell...
return cell;
}
【问题讨论】:
-
您真的不应该依赖
retainCount值。更多解释见this question。 -
我会更强烈地说:
retainCount比没用更糟糕。你不能相信它可以帮助你,因为它很可能带你走错路和/或迷惑你。根据内存管理编程指南的规定考虑所有权,如果您必须查看保留和释放(仅用于调试目的!),请改用 Instruments 的 Leaks 模板。
标签: iphone cocoa memory-management