From ef7c13c313463206b00c13cff7c76a3bdd4caeca Mon Sep 17 00:00:00 2001 From: lx-s Date: Wed, 8 Jul 2026 10:40:56 +0200 Subject: [PATCH] Use default initializers. Remove superfluous constructor --- WinMute/VistaAudio.cpp | 30 +++++++++++------------------- WinMute/WinAudio.h | 4 ++-- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/WinMute/VistaAudio.cpp b/WinMute/VistaAudio.cpp index 2db0e32..c6e928f 100644 --- a/WinMute/VistaAudio.cpp +++ b/WinMute/VistaAudio.cpp @@ -37,14 +37,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "WinAudio.h" -#define IF_FAILED_JUMP(hResult, ExitLabel) if (FAILED(hr)) { goto ExitLabel; } - -Endpoint::Endpoint() - : endpointVolume(nullptr), wasapiAudioEvents(nullptr), wasMuted(false) -{ - deviceName[0] = _T('\0'); -} - Endpoint::~Endpoint() { if (sessionCtrl && wasapiAudioEvents != nullptr) { @@ -78,16 +70,19 @@ bool VistaAudio::LoadAllEndpoints() eRender, DEVICE_STATE_ACTIVE, &audioEndpoints); - IF_FAILED_JUMP(hr, exit_error); + if (FAILED(hr)) { + return false; + } UINT epCount; hr = audioEndpoints->GetCount(&epCount); - IF_FAILED_JUMP(hr, exit_error); + if (FAILED(hr)) { + return false; + } for (UINT i = 0; i < epCount; ++i) { std::unique_ptr ep = std::make_unique(); CComPtr device = nullptr; - CComPtr propStore; hr = audioEndpoints->Item(i, &device); if (FAILED(hr)) { @@ -138,9 +133,6 @@ bool VistaAudio::LoadAllEndpoints() } return true; - -exit_error: - return false; } bool VistaAudio::Init(HWND hParent) @@ -159,10 +151,10 @@ bool VistaAudio::Init(HWND hParent) LoadAllEndpoints(); - mmnAudioEvents_ = new MMNotificationClient(this); - if (mmnAudioEvents_) { - deviceEnumerator_->RegisterEndpointNotificationCallback(mmnAudioEvents_); - } + // Attach: the client is created with a refcount of 1; a plain assignment + // would AddRef it to 2 and Uninit's single Release would leak it. + mmnAudioEvents_.Attach(new MMNotificationClient(this)); + deviceEnumerator_->RegisterEndpointNotificationCallback(mmnAudioEvents_); return true; } @@ -257,7 +249,7 @@ bool VistaAudio::RestoreMuteStatus() e->deviceName.c_str()); if (e->wasMuted != true) { if (FAILED(e->endpointVolume->SetMute(false, nullptr))) { - log.LogError(_T("Failed to restore mute status to %s for \"%s\""), + log.LogError(L"Failed to restore mute status to %s for \"%s\"", (e->wasMuted) ? L"true" : L"false", e->deviceName.c_str()); success = false; diff --git a/WinMute/WinAudio.h b/WinMute/WinAudio.h index 82a7572..a16c871 100644 --- a/WinMute/WinAudio.h +++ b/WinMute/WinAudio.h @@ -60,9 +60,9 @@ struct Endpoint { CComPtr sessionCtrl; std::unique_ptr wasapiAudioEvents; - bool wasMuted; + bool wasMuted = false; - Endpoint(); + Endpoint() = default; ~Endpoint(); Endpoint(const Endpoint&) = delete; Endpoint& operator=(const Endpoint&) = delete; -- 2.43.0