Properly clean up observers

This commit is contained in:
Matthias Neeracher 2007-04-16 05:35:52 +00:00
parent f41c28ff25
commit b6c9141e52
4 changed files with 30 additions and 3 deletions

View File

@ -26,6 +26,7 @@
NSNumber * songTempo;
NSString * tmpPath;
NSFileWrapper * vcsWrapper;
NSMutableArray* observers;
int repeatVolta;
bool brandNew;
@ -59,6 +60,7 @@
- (void) changedFileWrapper;
- (void) willChangeSong;
- (void) didChangeSong;
- (void) addObserver:(id)observer;
@end

View File

@ -74,6 +74,7 @@
vcsWrapper = nil;
repeatVolta = 2;
brandNew = true;
observers = [[NSMutableArray alloc] init];
[self setHasUndoManager:YES];
undo =
[[VLKeyValueUndo alloc] initWithOwner:self
@ -89,6 +90,17 @@
return self;
}
- (void) addObserver:(id)observer
{
[observers addObject:observer];
}
- (void) close
{
[observers makeObjectsPerformSelector:@selector(removeObservers:) withObject:self];
[super close];
}
- (void) dealloc
{
delete song;
@ -100,7 +112,8 @@
[songArranger release];
[vcsWrapper release];
[undo release];
[observers release];
if (tmpPath) {
[[NSFileManager defaultManager] removeFileAtPath:tmpPath handler:nil];
[tmpPath release];

View File

@ -16,6 +16,7 @@
owner = o;
keysAndNames = [kn retain];
[owner addObserver:self];
for (NSEnumerator * e = [keysAndNames keyEnumerator];
NSString * key = [e nextObject];
)
@ -26,13 +27,16 @@
return self;
}
- (void) dealloc
- (void)removeObservers:(id)target
{
for (NSEnumerator * e = [keysAndNames keyEnumerator];
NSString * key = [e nextObject];
)
[owner removeObserver:self forKeyPath:key];
[target removeObserver:self forKeyPath:key];
}
- (void) dealloc
{
[keysAndNames release];
[super dealloc];
}

View File

@ -891,12 +891,20 @@ static int8_t sSharpAcc[] = {
- (void)awakeFromNib
{
[[self document] addObserver:self];
[[self document] addObserver:self forKeyPath:@"song" options:0 context:nil];
[[self document] addObserver:self forKeyPath:@"songKey" options:0 context:nil];
[[self document] addObserver:self forKeyPath:@"songGroove" options:0 context:nil];
[self setGrooveMenu:[[self document] valueForKey:@"songGroove"]];
}
- (void)removeObservers:(id)target
{
[target removeObserver:self forKeyPath:@"song"];
[target removeObserver:self forKeyPath:@"songKey"];
[target removeObserver:self forKeyPath:@"songGroove"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)o change:(NSDictionary *)c context:(id)ctx
{
if ([keyPath isEqual:@"songKey"]) {