Skip to content

Commit

Permalink
Use a smart pointer to auto-free some WinAPI memory
Browse files Browse the repository at this point in the history
  • Loading branch information
kcat committed Nov 15, 2024
1 parent c58f30a commit f75f8f2
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions utils/alsoft-config/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <array>
#include <cmath>
#include <memory>

#include <QFileDialog>
#include <QMessageBox>
Expand Down Expand Up @@ -146,19 +147,25 @@ constexpr std::array hrtfModeList{
NameValuePair{ "Full", "full" },
};

#ifdef Q_OS_WIN32
struct CoTaskMemDeleter {
void operator()(void *buffer) { CoTaskMemFree(buffer); }
};
/* NOLINTNEXTLINE(*-avoid-c-arrays) */
using WCharBufferPtr = std::unique_ptr<WCHAR[],CoTaskMemDeleter>;
#endif

QString getDefaultConfigName()
{
#ifdef Q_OS_WIN32
const char *fname{"alsoft.ini"};
auto get_appdata_path = []() noexcept -> QString
static constexpr auto get_appdata_path = []() -> QString
{
QString ret;
WCHAR *buffer{};
auto buffer = WCharBufferPtr{};
if(const HRESULT hr{SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DONT_UNEXPAND,
nullptr, &buffer)}; SUCCEEDED(hr))
ret = QString::fromWCharArray(buffer);
CoTaskMemFree(buffer);
return ret;
nullptr, al::out_ptr(buffer))}; SUCCEEDED(hr))
return QString::fromWCharArray(buffer.get());
return QString{};
};
QString base = get_appdata_path();
#else
Expand All @@ -179,15 +186,13 @@ QString getDefaultConfigName()
QString getBaseDataPath()
{
#ifdef Q_OS_WIN32
auto get_appdata_path = []() noexcept -> QString
static constexpr auto get_appdata_path = []() -> QString
{
QString ret;
WCHAR *buffer{};
auto buffer = WCharBufferPtr{};
if(const HRESULT hr{SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DONT_UNEXPAND,
nullptr, &buffer)}; SUCCEEDED(hr))
ret = QString::fromWCharArray(buffer);
CoTaskMemFree(buffer);
return ret;
nullptr, al::out_ptr(buffer))}; SUCCEEDED(hr))
return QString::fromWCharArray(buffer.get());
return QString{};
};
QString base = get_appdata_path();
#else
Expand Down

0 comments on commit f75f8f2

Please sign in to comment.