0

I found when I slide a tableview then I pressed back button, the project crashed and the message is :message sent to deallocated instance. So I add - (void)dealloc { [self.tableView setDelegate:nil]; } to my project and the crash doesn't happen again. But I still can't understand why the project will crash if I didn't add the code mentioned above. The delegate is deallocated and I still sent message to it? Could anyone teach me about this. Following is the code in scrollview did scroll method.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat y = scrollView.contentOffset.y;
    [self.navigationController.navigationBar lt_setBackgroundColor:GWPRGBColor(255, 255, 255, ((y-100)/100.0f))];
}
RoyChen
  • 1
  • 5
  • If you're using the newest SDK, it shouldn't happen because the `delegate` is `weak`. You can check it in UITableView.h: `@property (nonatomic, weak, nullable) id delegate;` – KudoCC May 25 '16 at 01:53
  • Thank you for your answer. As you say the delegate is weak and I found the crash only happen in devices which are under iOS 9 version. – RoyChen May 25 '16 at 02:15
  • 1
    The older SDK use `assign` instead of `weak`, it turns out that `UIViewController ` is deallocated before the `UITableView`, so UITableView may send message to its delegate which is deallocated, that causes the crash. – KudoCC May 25 '16 at 02:25
  • I got it, thank you very much! – RoyChen May 25 '16 at 02:43
  • I'm curious to know if it would crash when you use the newest SDK to build and deploy to devices which are below iOS 9. Can you have a try? – KudoCC May 25 '16 at 02:50
  • What is the newest SDK you mentioned? – RoyChen May 25 '16 at 03:24
  • It's iOS 9.3, you can see it in your Xcode, select your target, check Build Settings -> Base SDK, if you're using the latest Xcode, it should be "Latest iOS (iOS 9.3)". – KudoCC May 25 '16 at 05:05
  • Yes, the base SDK is "Latest iOS (iOS 9.3)". And it still crash if I doesn't set delegate to nil in dealloc method. I think the reason is what you said " UIViewController is deallocated before the UITableView, so UITableView may send message to its delegate which is deallocated" – RoyChen May 25 '16 at 05:57

0 Answers0