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));
#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