UIScrollView EXC_BAD_ACCESS crash in iOS SDK

前端 未结 9 1599
暖寄归人
暖寄归人 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!

    0 讨论(0)
  • 2020-12-24 12:07

    All above didn't fix my issue, so I dug deep my code again. I recognized that crash appears when I perform keyboard and UICollectionView animation (yeah, it's a chat) and dismiss current controller.

    Application crashes because I try making scrolling in animation completion block :)

    Just cut it and all work well now!

    Happy coding and debugging :)

    0 讨论(0)
  • 2020-12-24 12:08

    I've seen such behaviour, when call scrollToRowAtIndexPath with not existing indexPath

    0 讨论(0)
  • 2020-12-24 12:09

    My guess would be that the scrollview's delegate is set to an object that has been deallocated. Try settings all the delegates of child objects to nil in your dealloc methods.

    0 讨论(0)
  • 2020-12-24 12:12

    After facing the same problem I set:

    self.collectionView.delegate = nil;
    

    in - (void)viewDidLoad (before actually setting ViewController as collectionView delegate) and -(void)viewWillDisappear:(BOOL)animated

    Everything works fine now.

    Thanks for help.

    0 讨论(0)
  • 2020-12-24 12:13

    The UIScrollView on stack frame #1 probably wants to inform its delegate about the animation ending, but the delegate is gone at that point. Setting NSZombieEnabled would probably confirm this.

    Delegates are not retained, so this is a common error in Cocoa and Cocoa Touch. Look for delegates on UIScrollView or UITableView in your code and try to find out which one might be released before its time.

    0 讨论(0)
提交回复
热议问题