开发者

EGOTableViewPullRefresh crash

Xcode 4 tells me that it crashes on this line of code: view.delegate = self;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.title = @"Blog";

    if (_refreshHeaderView == nil) {

        EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc]开发者_Go百科 initWithFrame:CGRectMake(0.0f, 0.0f - self.tableView.bounds.size.height, self.view.frame.size.width, self.tableView.bounds.size.height)];
        view.delegate = self;
        [self.tableView addSubview:view];
        _refreshHeaderView = view;
        [view release]; 
    }
    [_refreshHeaderView refreshLastUpdatedDate];
}

Do you have any idea why it crashes?


You're over-releasing the view. You init view, you then assign it to _refreshHeaderView, and then you immediately release view.

When you do this:

_refreshHeaderView = view;

...you're telling _refreshHeaderView to point to the memory location that view corresponds to. view has a retain count of 1, because you've alloc/init'd it. And then in the next line you release view, which means its retain count is 0, so the object no longer exists (I am simplifying here: you also add it as a subview, which would increment the retain count. But the point is if you don't need to release it here).

That also means _refreshHeaderView no longer exists either, because both view and _refreshHeadView are the same object. So when you call refreshLastUpdatedDate you'll get a bad access, and a crash.

Getting rid of the [view release] should stop the crash, but you should be very careful to make sure you release that object later on when you're done with it (presumably in your dealloc method). It would be advisable to make refreshHeaderView a property to help with this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜