0

I have a table which is filled with an array of objects, which I am observing, and when I delete all the objects, I remove the observer , but the problem is that when I delete all the objects in array and then again start adding it to the array I get removeObserver issue.

I have a strong reference to my object

I am adding Observer this way

[self.object addObserver:self forKeyPath:kTaskCompletedKey options:NSKeyValueObservingOptionNew context:&kTaskObservationContext];

and I am removing it this way

- (void)dealloc;
{
    [self.object removeObserver:self forKeyPath:kTaskCompletedKey context:&kTaskObservationContext];    
}

and also when I delete the object in the table using the delete method

I tried setting a breakpoint using NSKVODeallocateBreak, and what I observed is that it stops that the line @sythesize object = m_object;and I dont understand what that means So, friends please help me out

Regards Ranjit

Ranjit
  • 4,576
  • 11
  • 62
  • 121

1 Answers1

3

You must remove the observation before deleting the object. After doing that there is some debugging message you can send the object that let's you log the current observers - send it then verify no observers. Then you can safely release the objects.

EDIT: if the object you are observing, you can add the log in its dealloced - it had better report no observers. So, add this to the dealloc of your observed object:

NSLog(@"Dealloc of %@ with observationInfo: %@", self, [self observationInfo]);

In your controller, just before you release the observed object (which I assume is done by removing it from the array), use this log:

id foo = [myArray objectAtIndex:whatever];
NSLog(@"Release %@ with observationInfo: %@", foo, [foo observationInfo]);

If you find you are releasing an object you are still observing, that's a problem. If an object is getting dealloced and its still being observed that's a problem too.

EDIT: Before you add an object to the array, test if its already there or not. If not, then observe it. If yes, then you know you already are observing it.

David H
  • 40,852
  • 12
  • 92
  • 138
  • Hi @David H thanks for your reply, I am removing the observation before I delete the object from the array, and one more thing since I have strong reference, I should not get removeObserver issue right? – Ranjit Sep 26 '12 at 11:53
  • Use the debug feature - no Xcode on my iPad, it's something like observeInfo. You can find it buried in the KVO page or coding page. I assume you destroy then recreate objects, issue is when old object is released you get an exception. – David H Sep 26 '12 at 11:58
  • Hi @David H Do you mean this NSKVODeallocateBreak, I checked with this it stops at "@synthesize object = m_object" – Ranjit Sep 26 '12 at 12:01
  • That's a spurious message - that line is not code it gets turned into code (the setter getter). – David H Sep 26 '12 at 12:42
  • Hi @David H when I delete the first object, In the debugger it says `Release with observationInfo: (null)` and later when I delete the remaining objects I get this `Release with observationInfo: ( Context: 0x2e4e0, Property: 0x8938530> )` Dont know what it means . please help – Ranjit Sep 26 '12 at 14:01
  • So the second message with it saying there was something observing it is your problem. At that time, when its going to be dealloced, it still has an observer (big problem!) Add lots more logging and find out why you added an observer but never deregistered. – David H Sep 26 '12 at 15:33
  • Hi @David H I think I got the problem, as I am dynamically adding the objects in the array when the user clicks on "Add" button and I say [tableView reloadData] because of that it always calls addObserver each time I add a newobject inot the array and say reloadData, but if I dont say reloadData, then the tableWill not get refreshed. – Ranjit Sep 27 '12 at 06:17