diff --git a/Sources/VLDocument.h b/Sources/VLDocument.h index 83c1243..39a84b8 100644 --- a/Sources/VLDocument.h +++ b/Sources/VLDocument.h @@ -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 diff --git a/Sources/VLDocument.mm b/Sources/VLDocument.mm index 5f253a1..150c5d9 100644 --- a/Sources/VLDocument.mm +++ b/Sources/VLDocument.mm @@ -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]; diff --git a/Sources/VLKeyValueUndo.mm b/Sources/VLKeyValueUndo.mm index e21c0f8..6e1239b 100644 --- a/Sources/VLKeyValueUndo.mm +++ b/Sources/VLKeyValueUndo.mm @@ -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]; } diff --git a/Sources/VLSheetView.mm b/Sources/VLSheetView.mm index ec43bb2..5019597 100644 --- a/Sources/VLSheetView.mm +++ b/Sources/VLSheetView.mm @@ -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"]) {