]> repos.lx-s.de Git - winmute.git/commitdiff
Use correct ccomptr for vistaaudio
authorlx-s <lx-s@users.noreply.github.com>
Wed, 8 Jul 2026 12:44:16 +0000 (14:44 +0200)
committerlx-s <lx-s@users.noreply.github.com>
Wed, 8 Jul 2026 12:44:16 +0000 (14:44 +0200)
WinMute/VistaAudio.cpp
WinMute/WinAudio.h

index c6e928ffe85aa5b95be20bf8d2b88a1b2f9407b5..7cef244a6284b8da1b282724afb70949ca3fdd33 100644 (file)
@@ -40,7 +40,7 @@ POSSIBILITY OF SUCH DAMAGE.
 Endpoint::~Endpoint()
 {
    if (sessionCtrl && wasapiAudioEvents != nullptr) {
-      sessionCtrl->UnregisterAudioSessionNotification(wasapiAudioEvents.get());
+      sessionCtrl->UnregisterAudioSessionNotification(wasapiAudioEvents);
    }
 }
 
@@ -115,11 +115,10 @@ bool VistaAudio::LoadAllEndpoints()
          continue;
       }
 
-      ep->wasapiAudioEvents = std::make_unique<VistaAudioSessionEvents>(this);
-      if (ep->wasapiAudioEvents) {
-         ep->sessionCtrl->RegisterAudioSessionNotification(
-            ep->wasapiAudioEvents.get());
-      }
+      // Attach: the CComPtr takes over the initial reference from new,
+      // so the refcount stays balanced.
+      ep->wasapiAudioEvents.Attach(new VistaAudioSessionEvents(this));
+      ep->sessionCtrl->RegisterAudioSessionNotification(ep->wasapiAudioEvents);
 
       hr = device->Activate(
          __uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER,
index a16c871dfea0acc9e0c8a1570264c66e9de7187b..e1efe4d3f4539555bb284053ed3db3a2e3507ea0 100644 (file)
@@ -58,7 +58,10 @@ struct Endpoint {
    std::wstring deviceName;
    CComPtr<IAudioEndpointVolume> endpointVolume;
    CComPtr<IAudioSessionControl> sessionCtrl;
-   std::unique_ptr<VistaAudioSessionEvents> wasapiAudioEvents;
+   // VistaAudioSessionEvents is a ref-counted COM object; holding it in a
+   // CComPtr keeps its lifetime tied to the COM refcount instead of fighting
+   // it with a second owner.
+   CComPtr<VistaAudioSessionEvents> wasapiAudioEvents;
 
    bool wasMuted = false;