안녕하세요 모두 sqllite에서 가져온 항목 목록이있는 UITableView가 있습니다. 그러나 뷰를 렌더링 할 때 메모리 누수가 발생합니다. 다음은 내 cellForRowAtIndexPath 메소드입니다.UITableView의 메모리 누수 문제
static NSString *CellIdentifier = @"BarListItemCell";
BarListItemViewCell *cell = (BarListItemViewCell *)[tableView
dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"BarListItemViewCell" owner:self options:nil];
for (id cellObject in nib) {
if ([cellObject isKindOfClass : [BarListItemViewCell class]]) {
cell = (BarListItemViewCell *) cellObject;
//break;
}
}
NSString * key = [keys objectAtIndex:[indexPath section]];
NSDictionary * unit = [[barListDataSource objectForKey:key] objectAtIndex:[indexPath row]];
NSLog(@"unit count is %d", [unit retainCount]);
cell.name.text = [unit objectForKey:@"name"];
cell.address.text = [unit objectForKey:@"address1"];
cell.features.text = [unit objectForKey:@"features"];
cell.logo.image = [UIImage imageWithData:[unit objectForKey:@"logo"]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
"NSLog (@ 단위 개수는 % d", [unit retainCount]) ""줄을 볼 수 있습니다. viewDidLoad 후에 콘솔에 3 줄의 "unit count is 2"(전체 화면에 3 개의 항목이 있음)가 표시되는 것은 매우 이상합니다. 하지만 UITableView가 다음 항목을 표시하도록 화면을 끌면 콘솔에 "unit count is 1"이 표시됩니다. [tableView reloadData] 메서드가 호출되면 콘솔에 "unit count is 1"이 표시됩니다. 그래서 그것은 UITableView 자동으로 데이터 소스를 릴리스 것 같습니다. 그래서 내가 유닛의 메모리 참조 카운트를 2로 유지하는 것이다. 그렇지 않으면 해제가 일어날 것이다. 그러나 비용은 유닛에 의해 점유 된 메모리가 결코 해제되지 않는다는 것입니다!
답장을 보내 주셔서 감사합니다. 먼저 메모리 관리 지침을 읽어 보겠습니다. 그러나 상황은 내가 항상 데이터 소스에 autorelease 메소드를 보낼 수 없다는 것을 의미하는 UITableView의 데이터 소스의 소유권을 가져야한다는 것입니다. 그러나 [tableView reloadData]가 호출되기 전에 데이터 소스 참조를 새 목록/사전에 할당합니다. 이전 데이터 소스를 먼저 수동으로 릴리스해야한다는 의미입니까? – Shenjun