From 10f0644407863368121cf1f902e1ff273fc334a3 Mon Sep 17 00:00:00 2001
From: Christopher Snowhill <kode54@gmail.com>
Date: Fri, 10 Jun 2022 18:43:56 -0700
Subject: [PATCH] [Core Audio] Conditionally uninitialize equalizer

Only uninitialize the equalizer if sound output was successfully started
and the equalizer AudioUnit was successfully ininitialized.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
---
 Audio/Output/OutputCoreAudio.h | 1 +
 Audio/Output/OutputCoreAudio.m | 7 ++++++-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/Audio/Output/OutputCoreAudio.h b/Audio/Output/OutputCoreAudio.h
index 5aef23443..f02c12020 100644
--- a/Audio/Output/OutputCoreAudio.h
+++ b/Audio/Output/OutputCoreAudio.h
@@ -51,6 +51,7 @@ using std::atomic_long;
 	BOOL restarted;
 
 	BOOL eqEnabled;
+	BOOL eqInitialized;
 
 	BOOL streamFormatStarted;
 
diff --git a/Audio/Output/OutputCoreAudio.m b/Audio/Output/OutputCoreAudio.m
index 92c2fb20e..b82d9d812 100644
--- a/Audio/Output/OutputCoreAudio.m
+++ b/Audio/Output/OutputCoreAudio.m
@@ -764,6 +764,8 @@ default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const
 	if(_err)
 		return NO;
 
+	eqInitialized = YES;
+
 	[self setEqualizerEnabled:[[[[NSUserDefaultsController sharedUserDefaultsController] defaults] objectForKey:@"GraphicEQenable"] boolValue]];
 
 	[outputController beginEqualizer:_eq];
@@ -851,7 +853,10 @@ default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const
 		}
 	if(_eq) {
 		[outputController endEqualizer:_eq];
-		AudioUnitUninitialize(_eq);
+		if(eqInitialized) {
+			AudioUnitUninitialize(_eq);
+			eqInitialized = NO;
+		}
 		AudioComponentInstanceDispose(_eq);
 		_eq = NULL;
 	}