diff --git a/src/corelib/io/qsettings_win.cpp b/src/corelib/io/qsettings_win.cpp index 979b37a0be0..b9ab84d580b 100644 --- a/src/corelib/io/qsettings_win.cpp +++ b/src/corelib/io/qsettings_win.cpp @@ -43,6 +43,7 @@ #include "qlist.h" #include "qmap.h" #include "qdebug.h" +#include "qscopeguard.h" #include // See "Accessing an Alternate Registry View" at: @@ -487,14 +488,14 @@ bool QWinSettingsPrivate::readKey(HKEY parentHandle, const QString &rSubKey, QVa if (handle == 0) return false; + const auto closeKey = qScopeGuard([handle] { RegCloseKey(handle); }); + // get the size and type of the value DWORD dataType; DWORD dataSize; LONG res = RegQueryValueEx(handle, reinterpret_cast(rSubkeyName.utf16()), 0, &dataType, 0, &dataSize); - if (res != ERROR_SUCCESS) { - RegCloseKey(handle); + if (res != ERROR_SUCCESS) return false; - } // workaround for rare cases where trailing '\0' are missing in registry if (dataType == REG_SZ || dataType == REG_EXPAND_SZ) @@ -506,10 +507,8 @@ bool QWinSettingsPrivate::readKey(HKEY parentHandle, const QString &rSubKey, QVa QByteArray data(dataSize, 0); res = RegQueryValueEx(handle, reinterpret_cast(rSubkeyName.utf16()), 0, 0, reinterpret_cast(data.data()), &dataSize); - if (res != ERROR_SUCCESS) { - RegCloseKey(handle); + if (res != ERROR_SUCCESS) return false; - } switch (dataType) { case REG_EXPAND_SZ: @@ -578,7 +577,6 @@ bool QWinSettingsPrivate::readKey(HKEY parentHandle, const QString &rSubKey, QVa break; } - RegCloseKey(handle); return true; }