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
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));
#if defined(Q_OS_WIN)
auto programWchar = std::make_unique<wchar_t[]>(program.length() + 1);
program.toWCharArray(programWchar.get());
const std::wstring programWStr = program.toStdWString();
// Need to split arguments manually because QProcess::startDetached(QString)
// will strip off empty parameters.
// E.g. `python.exe "1" "" "3"` will become `python.exe "1" "3"`
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;
for (int i = 1; i < argCount; ++i)
@@ -836,8 +835,9 @@ void Application::cleanup()
m_window->hide();
#ifdef Q_OS_WIN
const std::wstring msg = tr("Saving torrent progress...").toStdWString();
::ShutdownBlockReasonCreate(reinterpret_cast<HWND>(m_window->effectiveWinId())
, tr("Saving torrent progress...").toStdWString().c_str());
, msg.c_str());
#endif // Q_OS_WIN
// 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)
mname += QString::number(idx);
const std::wstring mnameWStr = mname.toStdWString();
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)
{
qErrnoWarning("QtLockedFile::lock(): CreateMutex failed");
@@ -100,7 +102,7 @@ Qt::HANDLE QtLockedFile::getMutexHandle(const int idx, const bool doCreate)
}
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 (GetLastError() != ERROR_FILE_NOT_FOUND)

View File

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