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

View File

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

View File

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

View File

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