EGOTableViewPullRefresh 崩溃
Xcode 4 告诉我它在这行代码处崩溃: view.delegate = self;
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
self.title = @"Blog";
if (_refreshHeaderView == nil) {
EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] 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];
}
您知道它崩溃的原因吗?
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] 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您过度释放了视图。您初始化
view
,然后将其分配给_refreshHeaderView
,然后立即释放view
。当您执行此操作时:
_refreshHeaderView = view;
...您是在告诉 _refreshHeaderView 指向
view
对应的内存位置。view
的保留计数为 1,因为您已经分配/初始化了它。然后在下一行中释放view
,这意味着它的保留计数为 0,因此该对象不再存在(我在这里简化:您还将它添加为子视图,这会增加保留但重点是如果你不需要在这里释放它)。这也意味着 _refreshHeaderView 也不再存在,因为视图和 _refreshHeadView 是同一个对象。因此,当您调用
refreshLastUpdatedDate
时,您将遇到访问错误并崩溃。摆脱
[view release]
应该可以阻止崩溃,但是您应该非常小心,以确保稍后在使用完该对象后释放该对象(大概是在 dealloc 方法中)。建议将refreshHeaderView
设置为一个属性来帮助解决此问题。You're over-releasing the view. You init
view
, you then assign it to_refreshHeaderView
, and then you immediately releaseview
.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 releaseview
, 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 makerefreshHeaderView
a property to help with this.