views:

111

answers:

1

I am having a crash in my CALayer subclass when I remove myself as an observer in -(void)dealloc:

- (void)dealloc {
 [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:@"showColorLabels"];
 [colorLabel release];
 [_color release];
 [super dealloc];
}

An exception is thrown. It says that self has not been added as an observer. This only happens in a certain case, after [CATransaction flush] is called.

I used Instruments to see when the object was allocated. It says it was allocated with the call CALayerGetPresentationLayer(). I am not sure how this works, but I guess this is a copy of my original layer, so init was never called, and I was never added as an observer.

How can I either check that I am an observer before removing myself, or maybe tell if I am a presentation layer?

Bridger Maxwell

A: 

Welllll.... to ask the obvious question in response:

Where are you adding self as an observer of the @"showColorLabels key?

You shouldn't have to invoke -removeObserver:forKeyPath: unless you explicitly added the object as an observer in the first place.

bbum
Yeah, I added myself as an observer in my init method. I think the problem is that when a copy of my layer is made my init method isn't called.
Bridgeyman
That would definitely be an issue; you should override `-copy` to set up the observers appropriately.
bbum