1

更新 - 一种解决方法

我通过这样做找到了解决方法:(但我仍然不明白为什么在这种情况下dealloc我们必须使用[_profileImage release];,即使我们不拥有,既不alloc也不new也不copy_profileImage

MyUITableView.m

- (void)dealloc {
    [_profileImage release];
    // and all other ivars get released here

    [super dealloc];
}

- (void)onClickLogoutButton {
    if (_profileImage != nil) {
        _profileImage = nil;
    }
    // and other operations
}

当我有[_profileImage release];in时会发生崩溃onClickLogoutButton,因为我不拥有(既不alloc也不new也不copy_profileImage,而只是用于_profileImage = [UIImage imageWithData:data];将对象传递给_profileImage

- (void)onClickLogoutButton {
    if (_profileImage != nil) {
        [_profileImage release];
        _profileImage = nil;
    }
    // and other operations
}

原始问题

以下代码在 Xcode 5、iOS 7 中使用手动保留释放 (MRR)。

ProfileCell *cell = (ProfileCell *)[tableView dequeueReusableCellWithIdentifier:identifierForProfileCell];导致崩溃,错误消息之一是Thread 1: EXC_BAD_ACCESS (code=2, address=0x2448c90c)

是不是因为我放错东西了?但我不确定何时、何地以及发布什么内容。项目中有一个注销功能,注销后我们应该释放一些东西还是让dealloc来完成这项工作?首次登录时发生错误,然后注销然后重新登录,滚动到配置文件单元格并崩溃。

我应该在注销后打电话[self reloadData];MyUITableView.m

MyUITableView.m

-(UITableViewCell *)tableView:(UITableView *)tableViewLeft cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //ProfileCell
    if ([indexPath section] == 0 && [indexPath row] == 0) {
        static NSString *identifierForProfileCell   = @"ProfileCell";

        ProfileCell *cell = (ProfileCell *)[tableViewLeft dequeueReusableCellWithIdentifier:identifierForProfileCell]; // This line causes crash: Thread 1: EXC_BAD_ACCESS (code=2, address=0x2448c90c)

        if (cell == nil) {
            cell = [[[ProfileCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierForProfileCell] autorelease];
        }

        [[cell textField] setText:_userID];
        if (_profileImage == nil && _profileURL != nil) {
            NSURL *url = [NSURL URLWithString:_profileURL];
            NSURLRequest* request = [NSURLRequest requestWithURL:url];
            [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * response, NSData * data, NSError * error) {
                    if (!error) {
                        _profileImage = [UIImage imageWithData:data];
                        [[cell iconView] setImage:_profileImage];
                    }
                }];
            }

            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSetting:)];
            [singleTap setNumberOfTapsRequired:1];
            [cell.settingWrapper addGestureRecognizer:singleTap];
            [singleTap release];
            return cell;
        }
    } else {
        // ....
    }
    return nil;
}
4

1 回答 1

4

你之前在tableView中注册过ProfileCell吗?

像这样在 viewDidLoad :

- (void)viewDidLoad
  {
    [super viewDidLoad];
    [self.tableView registerClass:ProfileCell  forCellReuseIdentifier:@"ProfileCell"];
  }
于 2013-11-04T15:56:22.077 回答