Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instance was deallocated while key value observers were still registered with it

I've got a UITableView.

Here I got different cell's. Each cell has a model. With KVO and NotificationCenter the cell listen to the model for changes. When I leave the ViewController I get this error:

An instance 0x109564200 of class Model was deallocated while key value observers were still registered with it. 
Observation info was leaked, and may even become mistakenly attached to some other object. 
Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:
<NSKeyValueObservationInfo 0x109429cc0> (
<NSKeyValueObservance 0x109429c50: Observer: 0x10942d1c0, Key path: name, Options: <New: NO, Old: NO, Prior: NO> Context: 0x0, Property: 0x10968fa00>
)

In the cell I do this when the model property is set/changed:

[_model addObserver:self
         forKeyPath:@"name"
            options:0
            context:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(modelIsInvalid:)
                                             name:@"modelIsInvalid"
                                           object:_model];

Then in the cell's dealloc:

- (void)dealloc
{
    NSLog(@"DEALLOC CELL");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_model removeObserver:self forKeyPath:@"name"];
}

In the model I also check when it get's deallocated:

- (void)dealloc
{
    NSLog(@"DEALLOC MODEL");
}

All cell's are deallocated before all the models, but still I get this error. Also I'm not sure how to set the breakpoint mentioned in the error.

like image 510
Johannes Avatar asked Jul 31 '14 10:07

Johannes


3 Answers

It won't work because the cells are being reused. So when the cell goes off the screen it's not deallocated, it goes to reuse pool.

You shouldn't register notifications and KVO in cell. You should do it in table view controller instead and when the model changes you should update model and reload visible cells.

like image 88
Greg Avatar answered Nov 11 '22 19:11

Greg


I found the answer. I can't delete the thread, someone has answered :) Maybe it will be useful for someone.

The problem is that the UITableView will dequeue the same cell used before, for a row longer down (that becomes visible when scrolling far enough).

In the setter I now have:

// Before we set new model
if (_model) {
    [_model removeObserver:self forKeyPath:@"name"];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"modelIsInvalid" object:_model];
}

_model = model;

[_model addObserver:self
         forKeyPath:@"name"
            options:0
            context:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(modelIsInvalid:)
                                             name:@"modelIsInvalid"
                                           object:_model];
like image 6
Johannes Avatar answered Nov 11 '22 19:11

Johannes


Based on the accepted answer (which is correct) you can solve it by removing the observer on the "prepareForReuse" method of the cell.

That method will be called before reusing the cell on scrolling etc.. thus you wont have any problem.

- (void)prepareForReuse{
    [_model removeObserver:self forKeyPath:@"name"];
}
like image 4
PakitoV Avatar answered Nov 11 '22 17:11

PakitoV