From 7bc49ccb80407af3041b89b3fd0fafb7dbaa25a1 Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Thu, 16 Jun 2022 18:16:09 -0700 Subject: [PATCH] [Event Handler] Fix observers for reused classes Fix class handling so it cleans up observers if the InputNode is reused. Signed-off-by: Christopher Snowhill --- Audio/Chain/InputNode.m | 4 ++++ Audio/CogPluginMulti.h | 1 + Audio/CogPluginMulti.m | 26 ++++++++++++++++---------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/Audio/Chain/InputNode.m b/Audio/Chain/InputNode.m index 021a90ea5..ae0d81bfb 100644 --- a/Audio/Chain/InputNode.m +++ b/Audio/Chain/InputNode.m @@ -33,6 +33,8 @@ static void *kInputNodeContext = &kInputNodeContext; } - (BOOL)openWithSource:(id)source { + [self removeObservers]; + decoder = [AudioDecoder audioDecoderForSource:source]; if(decoder == nil) @@ -63,6 +65,8 @@ static void *kInputNodeContext = &kInputNodeContext; } - (BOOL)openWithDecoder:(id)d { + [self removeObservers]; + DLog(@"Opening with old decoder: %@", d); decoder = d; diff --git a/Audio/CogPluginMulti.h b/Audio/CogPluginMulti.h index 6eac16c7e..0fed368e4 100644 --- a/Audio/CogPluginMulti.h +++ b/Audio/CogPluginMulti.h @@ -12,6 +12,7 @@ @interface CogDecoderMulti : NSObject { NSArray *theDecoders; id theDecoder; + BOOL observersAdded; } - (id)initWithDecoders:(NSArray *)decoders; diff --git a/Audio/CogPluginMulti.m b/Audio/CogPluginMulti.m index 879705223..54834a0c3 100644 --- a/Audio/CogPluginMulti.m +++ b/Audio/CogPluginMulti.m @@ -113,20 +113,26 @@ static void *kCogDecoderMultiContext = &kCogDecoderMultiContext; } - (void)registerObservers { - [theDecoder addObserver:self - forKeyPath:@"properties" - options:(NSKeyValueObservingOptionNew) - context:kCogDecoderMultiContext]; + if(!observersAdded) { + [theDecoder addObserver:self + forKeyPath:@"properties" + options:(NSKeyValueObservingOptionNew) + context:kCogDecoderMultiContext]; - [theDecoder addObserver:self - forKeyPath:@"metadata" - options:(NSKeyValueObservingOptionNew) - context:kCogDecoderMultiContext]; + [theDecoder addObserver:self + forKeyPath:@"metadata" + options:(NSKeyValueObservingOptionNew) + context:kCogDecoderMultiContext]; + observersAdded = YES; + } } - (void)removeObservers { - [theDecoder removeObserver:self forKeyPath:@"properties" context:kCogDecoderMultiContext]; - [theDecoder removeObserver:self forKeyPath:@"metadata" context:kCogDecoderMultiContext]; + if(observersAdded) { + observersAdded = NO; + [theDecoder removeObserver:self forKeyPath:@"properties" context:kCogDecoderMultiContext]; + [theDecoder removeObserver:self forKeyPath:@"metadata" context:kCogDecoderMultiContext]; + } } - (BOOL)setTrack:(NSURL *)track {