1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-11 07:18:08 +00:00

Don't use explicit memory management

And avoid dangling pointers.
This commit is contained in:
Chocobo1 2022-03-23 15:27:47 +08:00
parent 8a708fd97e
commit bbd936fdfa
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
9 changed files with 28 additions and 36 deletions

View File

@ -403,14 +403,13 @@ void Application::runExternalProgram(const BitTorrent::Torrent *torrent) const
LogMsg(tr("Torrent: %1, running external program, command: %2").arg(torrent->name(), program)); LogMsg(tr("Torrent: %1, running external program, command: %2").arg(torrent->name(), program));
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
auto programWchar = std::make_unique<wchar_t[]>(program.length() + 1); const std::wstring programWStr = program.toStdWString();
program.toWCharArray(programWchar.get());
// Need to split arguments manually because QProcess::startDetached(QString) // Need to split arguments manually because QProcess::startDetached(QString)
// will strip off empty parameters. // will strip off empty parameters.
// E.g. `python.exe "1" "" "3"` will become `python.exe "1" "3"` // E.g. `python.exe "1" "" "3"` will become `python.exe "1" "3"`
int argCount = 0; int argCount = 0;
std::unique_ptr<LPWSTR[], decltype(&::LocalFree)> args {::CommandLineToArgvW(programWchar.get(), &argCount), ::LocalFree}; std::unique_ptr<LPWSTR[], decltype(&::LocalFree)> args {::CommandLineToArgvW(programWStr.c_str(), &argCount), ::LocalFree};
QStringList argList; QStringList argList;
for (int i = 1; i < argCount; ++i) for (int i = 1; i < argCount; ++i)
@ -836,8 +835,9 @@ void Application::cleanup()
m_window->hide(); m_window->hide();
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
const std::wstring msg = tr("Saving torrent progress...").toStdWString();
::ShutdownBlockReasonCreate(reinterpret_cast<HWND>(m_window->effectiveWinId()) ::ShutdownBlockReasonCreate(reinterpret_cast<HWND>(m_window->effectiveWinId())
, tr("Saving torrent progress...").toStdWString().c_str()); , msg.c_str());
#endif // Q_OS_WIN #endif // Q_OS_WIN
// Do manual cleanup in MainWindow to force widgets // Do manual cleanup in MainWindow to force widgets

View File

@ -87,9 +87,11 @@ Qt::HANDLE QtLockedFile::getMutexHandle(const int idx, const bool doCreate)
if (idx >= 0) if (idx >= 0)
mname += QString::number(idx); mname += QString::number(idx);
const std::wstring mnameWStr = mname.toStdWString();
if (doCreate) if (doCreate)
{ {
const Qt::HANDLE mutex = ::CreateMutexW(NULL, FALSE, reinterpret_cast<const TCHAR *>(mname.utf16())); const Qt::HANDLE mutex = ::CreateMutexW(NULL, FALSE, mnameWStr.c_str());
if (!mutex) if (!mutex)
{ {
qErrnoWarning("QtLockedFile::lock(): CreateMutex failed"); qErrnoWarning("QtLockedFile::lock(): CreateMutex failed");
@ -100,7 +102,7 @@ Qt::HANDLE QtLockedFile::getMutexHandle(const int idx, const bool doCreate)
} }
else else
{ {
const Qt::HANDLE mutex = ::OpenMutexW((SYNCHRONIZE | MUTEX_MODIFY_STATE), FALSE, reinterpret_cast<const TCHAR *>(mname.utf16())); const Qt::HANDLE mutex = ::OpenMutexW((SYNCHRONIZE | MUTEX_MODIFY_STATE), FALSE, mnameWStr.c_str());
if (!mutex) if (!mutex)
{ {
if (GetLastError() != ERROR_FILE_NOT_FOUND) if (GetLastError() != ERROR_FILE_NOT_FOUND)

View File

@ -257,9 +257,10 @@ const QString straceWin::getBacktrace()
QTextStream logStream(&log); QTextStream logStream(&log);
logStream << "```\n"; logStream << "```\n";
const std::wstring appPath = QCoreApplication::applicationDirPath().toStdWString();
HANDLE hProcess = GetCurrentProcess(); HANDLE hProcess = GetCurrentProcess();
HANDLE hThread = GetCurrentThread(); HANDLE hThread = GetCurrentThread();
SymInitializeW(hProcess, QCoreApplication::applicationDirPath().toStdWString().c_str(), TRUE); SymInitializeW(hProcess, appPath.c_str(), TRUE);
DWORD64 dwDisplacement; DWORD64 dwDisplacement;

View File

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

View File

@ -128,26 +128,17 @@ namespace
QString getRegValue(const HKEY handle, const QString &name = {}) QString getRegValue(const HKEY handle, const QString &name = {})
{ {
QString result; const std::wstring nameWStr = name.toStdWString();
DWORD type = 0; DWORD type = 0;
DWORD cbData = 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 // 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; DWORD cBuffer = (cbData / sizeof(WCHAR)) + 1;
LPWSTR lpData = new WCHAR[cBuffer]; LPWSTR lpData = new WCHAR[cBuffer];
LONG res = ::RegQueryValueExW(handle, lpValueName, NULL, &type, (LPBYTE)lpData, &cbData); LONG res = ::RegQueryValueExW(handle, nameWStr.c_str(), NULL, &type, (LPBYTE)lpData, &cbData);
if (lpValueName)
delete[] lpValueName;
QString result;
if (res == ERROR_SUCCESS) if (res == ERROR_SUCCESS)
{ {
lpData[cBuffer - 1] = 0; lpData[cBuffer - 1] = 0;
@ -186,18 +177,14 @@ namespace
bool found = false; bool found = false;
while (!found && !versions.empty()) while (!found && !versions.empty())
{ {
const QString version = versions.takeLast() + u"\\InstallPath"; const std::wstring version = QString(versions.takeLast() + u"\\InstallPath").toStdWString();
LPWSTR lpSubkey = new WCHAR[version.size() + 1];
version.toWCharArray(lpSubkey);
lpSubkey[version.size()] = 0;
HKEY hkInstallPath; HKEY hkInstallPath;
res = ::RegOpenKeyExW(hkPythonCore, lpSubkey, 0, samDesired, &hkInstallPath); res = ::RegOpenKeyExW(hkPythonCore, version.c_str(), 0, samDesired, &hkInstallPath);
delete[] lpSubkey;
if (res == ERROR_SUCCESS) 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); path = getRegValue(hkInstallPath);
::RegCloseKey(hkInstallPath); ::RegCloseKey(hkInstallPath);

View File

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

View File

@ -31,7 +31,6 @@
#include <QtGlobal> #include <QtGlobal>
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <memory>
#include <Windows.h> #include <Windows.h>
#endif #endif
@ -92,9 +91,8 @@ namespace Utils::Misc
template <typename T> template <typename T>
T loadWinAPI(const QString &source, const char *funcName) T loadWinAPI(const QString &source, const char *funcName)
{ {
const QString path = (windowsSystemPath() / Path(source)).toString(); const std::wstring path = (windowsSystemPath() / Path(source)).toString().toStdWString();
return reinterpret_cast<T>(::GetProcAddress(::LoadLibraryW( return reinterpret_cast<T>(::GetProcAddress(::LoadLibraryW(path.c_str()), funcName));
path.toStdWString().c_str()), funcName));
} }
#endif // Q_OS_WIN #endif // Q_OS_WIN
} }

View File

@ -114,9 +114,9 @@ namespace
{ {
QPixmap pixmapForExtension(const QString &ext) const override QPixmap pixmapForExtension(const QString &ext) const override
{ {
const QString extWithDot = QLatin1Char('.') + ext; const std::wstring extWStr = QString(u'.' + ext).toStdWString();
SHFILEINFO sfi {}; SHFILEINFO sfi {};
HRESULT hr = ::SHGetFileInfoW(extWithDot.toStdWString().c_str(), HRESULT hr = ::SHGetFileInfoW(extWStr.c_str(),
FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(sfi), SHGFI_ICON | SHGFI_USEFILEATTRIBUTES); FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(sfi), SHGFI_ICON | SHGFI_USEFILEATTRIBUTES);
if (FAILED(hr)) if (FAILED(hr))
return {}; return {};

View File

@ -164,7 +164,8 @@ void Utils::Gui::openFolderSelect(const Path &path)
{ {
if (SUCCEEDED(::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE))) if (SUCCEEDED(::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE)))
{ {
PIDLIST_ABSOLUTE pidl = ::ILCreateFromPathW(reinterpret_cast<const wchar_t *>(path.toString().utf16())); const std::wstring pathWStr = path.toString().toStdWString();
PIDLIST_ABSOLUTE pidl = ::ILCreateFromPathW(pathWStr.c_str());
if (pidl) if (pidl)
{ {
::SHOpenFolderAndSelectItems(pidl, 0, nullptr, 0); ::SHOpenFolderAndSelectItems(pidl, 0, nullptr, 0);