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
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!
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 :)
I've seen such behaviour, when call scrollToRowAtIndexPath with not existing indexPath
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.
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.
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.