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 fl开发者_如何学编程ush] 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
I found a good solution here: https://stackoverflow.com/a/6714561/958017
you can use a try-catch block in your call to removeobserver:
@try{
[[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:@"showColorLabels"];
}@catch(id anException){
//Whatever you want to do.
}
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.
精彩评论