UIScrollView EXC_BAD_ACCESS crash in iOS SDK

前端 未结 9 1635
暖寄归人
暖寄归人 2020-12-24 11:49

I have an iPhone SDK application that has several views that appear and disappear as the user creates content. After using the application on a device for a while, I get th

9条回答
  •  失恋的感觉
    2020-12-24 12:05

    At first, delegates should be of weak/assign type. But event in this case there is a very common subtle obstacle driven by scroll animations. If you use animated content offset changes for your ScrollViews you strongly need to set its delegate to nil at dealloc method.

    Otherwise you will get the following

    [YourViewController respondsToSelector:]: message sent to deallocated instance
    

    The very common example:

    1. _tableView is ivar of YourViewController
    2. _tableView.delegate = self;
    3. - (void)scrollViewDidScroll:(UIScrollView *)scrollView is implemented at YourViewController
    4. at some point you call [_tableView scrollToRowAtIndexPath:indexPath 
       atScrollPosition:UITableViewScrollPositionBottom animated:YES];
       or [_tableView setContentOffset:CGPoint animated:YES]
       and try to close YourViewController
    

    The _tableView is retained by CoreAnimation, but YourViewController is deallocated!

提交回复
热议问题