Endpoint::~Endpoint()
{
if (sessionCtrl && wasapiAudioEvents != nullptr) {
- sessionCtrl->UnregisterAudioSessionNotification(wasapiAudioEvents.get());
+ sessionCtrl->UnregisterAudioSessionNotification(wasapiAudioEvents);
}
}
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,
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;