Skip to content

Commit

Permalink
nil out tableView delegate on dealloc
Browse files Browse the repository at this point in the history
If tableView is scrolling as this VC is being dealloc'd
it continues to send messages (scrollViewDidScroll:) to its delegate.
This is fine if the delegate will outlive tableView (e.g. this VC would.)
However, if the delegate is an instance that may be dealloc'd
before the tableView
(i.e. _scrollProxy may be dealloc'd prior to tableView being dealloc'd)
the tableView will send messages to its delegate,
which is defined with an "assign" (i.e. unsafe_unretained) property.
This is a msgSend to non-nil'ed, invalid memory leading to a crash.
If or when UIScrollView's delegate is referred to with "weak" rather
than "assign", this can and should be removed.
  • Loading branch information
Jan Sichermann committed Apr 7, 2014
1 parent e1b9b93 commit 9a3adbe
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions DemoApp/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ @interface ViewController ()

@implementation ViewController

- (void)dealloc
{
// If tableView is scrolling as this VC is being dealloc'd
// it continues to send messages (scrollViewDidScroll:) to its delegate.
// This is fine if the delegate will outlive tableView (e.g. this VC would.)
// However, if the delegate is an instance that may be dealloc'd
// before the tableView
// (i.e. _scrollProxy may be dealloc'd prior to tableView being dealloc'd)
// the tableView will send messages to its delegate,
// which is defined with an "assign" (i.e. unsafe_unretained) property.
// This is a msgSend to non-nil'ed, invalid memory leading to a crash.
// If or when UIScrollView's delegate is referred to with "weak" rather
// than "assign", this can and should be removed.
self.tableView.delegate = nil;
}

- (void)viewDidLoad
{
[super viewDidLoad];
Expand Down

0 comments on commit 9a3adbe

Please sign in to comment.