Help with crash in removeObserver:forKeyPath:
Help with crash in removeObserver:forKeyPath:
- Subject: Help with crash in removeObserver:forKeyPath:
- From: "Ilja A. Iwas" <email@hidden>
- Date: Wed, 14 Sep 2005 21:59:16 +0200
Hi there,
I'm having a hard time tracking down a crash related
[removeObserver:forKeyPath:]. Unfortunately the bug is not
reproducable, I've only seen various crash logs looking like this one:
0 com.apple.CoreFoundation 0x9072dd84 CFRetain + 60
1 com.apple.Foundation 0x929094b0
_NSKeyValueObservationInfoCreateByRemoving + 440
2 com.apple.Foundation 0x92909288 -[NSObject
(NSKeyValueObserverRegistration) _removeObserver:forProperty:] + 56
3 com.apple.Foundation 0x92909178 -[NSObject
(NSKeyValueObserverRegistration) removeObserver:forKeyPath:] + 436
4 com.mydomain.myapp 0x00077d2c -[GSTreeController
removeItems:] + 368 (crt.c:300)
5 com.mydomain.myapp 0x00077b8c -[GSTreeController
deleteItems:] + 32 (crt.c:300)
6 com.mydomain.myapp 0x00087808 -[GSAppController
(ImageView) imageView:shouldDeleteObjects:] + 68 (crt.c:300)
7 com.mydomain.myapp 0x000868b8 -[GSImageView delete:] + 136
(crt.c:300)
8 com.mydomain.myapp 0x0008697c -[GSImageView keyDown:] +
108 (crt.c:300)
9 com.apple.AppKit 0x936600d8 -[NSWindow sendEvent:] + 6424
10 com.apple.AppKit 0x93608bfc -[NSApplication
sendEvent:] + 4172
11 com.apple.AppKit 0x93600090 -[NSApplication run] + 508
12 com.apple.AppKit 0x936f08bc NSApplicationMain + 452
13 com.mydomain.myapp 0x00002abc _start + 392 (crt.c:267)
14 com.mydomain.myapp 0x00002930 start + 48
Since I've been hunting down this bug for quite some time now without
success, maybe someone on this list can have a look at the code and
point me to what I'm doing wrong.
I have a tree controller class, which manages an outline view
consisting of simple items and groups of those items. My tree
controller class also manages an NSArrayController, which is used to
bind various user interface elements to. The content array of this
array controller contains all simple items, but not the groups. The
tree controller class observers the 'title' attribute of the simple
items, so it can reload the outline view when the user changes an
item's title.
My first thought was that the item which is going to get its observer
removed has already been released and therefore is invalid at the
point removeObserver:forKeyPath: is invoked. But the item is unbound
before it is getting removed from the the tree hierarchy and the
allItems array. So the retain count should be 2 at the crash time.
Thanks for any hints or tips,
Ilja
Here is that code that is supposed to cause the crash according to
the crash log:
- (void) removeBindingsForItem:(id) inItem
{
[inItem removeObserver:self forKeyPath:@"title"];
}
- (void) deleteItems:(NSArray*) inItems
{
[self removeItems:inItems];
[self postSelectionChangedNotification];
}
- (void) removeItems:(NSArray*) inItems
{
NSEnumerator *itemEnum = [inItems objectEnumerator];
id itemToRemove;
while (nil != (itemToRemove = [itemEnum nextObject]))
{
GSTreeItemGroup *parent;
parent = [self parentGroupForItem:itemToRemove];
NSAssert (parent, @"couldn't get parent for item");
if ([itemToRemove isKindOfClass:[GSTreeItemGroup class]]) //
if its a group, manually unbind leaf items and remove them from the
allItems array afterwards
{
NSArray *subitems = [itemToRemove
allPlainItemsDeep];
NSEnumerator *e = [subitems objectEnumerator];
id theSubitem;
while (nil != (theSubitem = [e nextObject]))
{
[self removeBindingsForItem:theSubitem];
}
[allItems removeObjectsInArray:subitems]; // remove from
array controllers content array
}
else // is a simple item
{
[self removeBindingsForItem:itemToRemove]; // has to be
invoked before [parent removeItem:]
[allItems removeObject:itemToRemove]; // remove from
array controllers content array
}
[parent removeItem:itemToRemove];
}
[outlineView reloadData];
[self synchronizeArrayControllerSelection];
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden