Skip to content

Commit

Permalink
Fix crash in SSLabel dealloc when there are no registered observers.
Browse files Browse the repository at this point in the history
The dealloc method attempts to remove the observers added in the initWithFrame method but if the initWithFrame has not been called such as when using a SSLabel in a view without a parent in a XIB then it crashes with an exception but it is not allowed to remove an observer than hasn't been added.

This fix does a simple check to see if there are any registered observers before attempting to remove them.
  • Loading branch information
daikini committed Feb 18, 2011
1 parent a134469 commit cabe99f
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions SSToolkit/SSLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ @implementation SSLabel
#pragma mark NSObject

- (void)dealloc {
[self removeObserver:self forKeyPath:@"verticalTextAlignment"];
[self removeObserver:self forKeyPath:@"textEdgeInsets"];
if ([self observationInfo] != nil) {
[self removeObserver:self forKeyPath:@"verticalTextAlignment"];
[self removeObserver:self forKeyPath:@"textEdgeInsets"];
}
[super dealloc];
}

Expand Down

2 comments on commit cabe99f

@soffes
Copy link

@soffes soffes commented on cabe99f Mar 3, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh clever. I don't use IB so I never experience these types of bugs. I'll go through everything and try to prevent this in all of my views. Good catch!

@daikini
Copy link
Owner Author

@daikini daikini commented on cabe99f Mar 9, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks!

Please sign in to comment.