Don't use explicit memory management

And avoid dangling pointers.
Original PR #16705.
This commit is contained in:
Chocobo1
2022-03-27 14:17:42 +08:00
committed by GitHub
parent ede7c8acbb
commit c621cae43b
9 changed files with 28 additions and 38 deletions

View File

@@ -279,8 +279,9 @@ namespace
if (!uuid.isNull())
return uuid.toString().toUpper(); // Libtorrent expects the GUID in uppercase
const std::wstring nameWStr = name.toStdWString();
NET_LUID luid {};
const LONG res = ::ConvertInterfaceNameToLuidW(name.toStdWString().c_str(), &luid);
const LONG res = ::ConvertInterfaceNameToLuidW(nameWStr.c_str(), &luid);
if (res == 0)
{
GUID guid;

View File

@@ -127,26 +127,17 @@ namespace
QString getRegValue(const HKEY handle, const QString &name = {})
{
QString result;
const std::wstring nameWStr = name.toStdWString();
DWORD type = 0;
DWORD cbData = 0;
LPWSTR lpValueName = NULL;
if (!name.isEmpty())
{
lpValueName = new WCHAR[name.size() + 1];
name.toWCharArray(lpValueName);
lpValueName[name.size()] = 0;
}
// Discover the size of the value
::RegQueryValueExW(handle, lpValueName, NULL, &type, NULL, &cbData);
::RegQueryValueExW(handle, nameWStr.c_str(), NULL, &type, NULL, &cbData);
DWORD cBuffer = (cbData / sizeof(WCHAR)) + 1;
LPWSTR lpData = new WCHAR[cBuffer];
LONG res = ::RegQueryValueExW(handle, lpValueName, NULL, &type, (LPBYTE)lpData, &cbData);
if (lpValueName)
delete[] lpValueName;
LONG res = ::RegQueryValueExW(handle, nameWStr.c_str(), NULL, &type, (LPBYTE)lpData, &cbData);
QString result;
if (res == ERROR_SUCCESS)
{
lpData[cBuffer - 1] = 0;
@@ -185,18 +176,14 @@ namespace
bool found = false;
while (!found && !versions.empty())
{
const QString version = versions.takeLast() + "\\InstallPath";
LPWSTR lpSubkey = new WCHAR[version.size() + 1];
version.toWCharArray(lpSubkey);
lpSubkey[version.size()] = 0;
const std::wstring version = QString(versions.takeLast() + "\\InstallPath").toStdWString();
HKEY hkInstallPath;
res = ::RegOpenKeyExW(hkPythonCore, lpSubkey, 0, samDesired, &hkInstallPath);
delete[] lpSubkey;
res = ::RegOpenKeyExW(hkPythonCore, version.c_str(), 0, samDesired, &hkInstallPath);
if (res == ERROR_SUCCESS)
{
qDebug("Detected possible Python v%s location", qUtf8Printable(version));
qDebug("Detected possible Python v%ls location", version.c_str());
path = getRegValue(hkInstallPath);
::RegCloseKey(hkInstallPath);

View File

@@ -31,6 +31,8 @@
#include <optional>
#ifdef Q_OS_WIN
#include <memory>
#include <windows.h>
#include <powrprof.h>
#include <Shlobj.h>
@@ -393,7 +395,7 @@ QString Utils::Misc::getUserIDString()
const int UNLEN = 256;
WCHAR buffer[UNLEN + 1] = {0};
DWORD buffer_len = sizeof(buffer) / sizeof(*buffer);
if (GetUserNameW(buffer, &buffer_len))
if (::GetUserNameW(buffer, &buffer_len))
uid = QString::fromWCharArray(buffer);
#else
uid = QString::number(getuid());

View File

@@ -31,7 +31,6 @@
#include <QtGlobal>
#ifdef Q_OS_WIN
#include <memory>
#include <Windows.h>
#endif
@@ -93,14 +92,11 @@ namespace Utils::Misc
QString path = windowsSystemPath();
if (!path.endsWith('\\'))
path += '\\';
path += source;
auto pathWchar = std::make_unique<wchar_t[]>(path.length() + 1);
path.toWCharArray(pathWchar.get());
const std::wstring pathWStr = path.toStdWString();
return reinterpret_cast<T>(
::GetProcAddress(::LoadLibraryW(pathWchar.get()), funcName));
::GetProcAddress(::LoadLibraryW(pathWStr.c_str()), funcName));
}
#endif // Q_OS_WIN
}