diff options
| author | LePresidente <brian.alexander.munro@gmail.com> | 2017-10-28 08:30:23 +0200 |
|---|---|---|
| committer | LePresidente <brian.alexander.munro@gmail.com> | 2017-10-28 08:30:23 +0200 |
| commit | ebe6011174767687f3b575c08132a720ecccbf55 (patch) | |
| tree | ac0ce2100b2fc4ce66f6f9278f51e57dbd82d12a /src/organizercore.cpp | |
| parent | 139ee36a3f0cc8b628d93edc890df19170adeac6 (diff) | |
Fixes the handle leak in the OrganizerCore::waitForProcessCompletion
Diffstat (limited to 'src/organizercore.cpp')
| -rw-r--r-- | src/organizercore.cpp | 99 |
1 files changed, 50 insertions, 49 deletions
diff --git a/src/organizercore.cpp b/src/organizercore.cpp index c55504a8..9ae8beae 100644 --- a/src/organizercore.cpp +++ b/src/organizercore.cpp @@ -1239,7 +1239,7 @@ bool OrganizerCore::waitForApplication(HANDLE handle, LPDWORD exitCode) bool OrganizerCore::waitForProcessCompletion(HANDLE handle, LPDWORD exitCode)
{
HANDLE processHandle = handle;
-
+
static const DWORD maxCount = 5;
size_t numProcesses = maxCount;
LPDWORD processes = new DWORD[maxCount];
@@ -1250,62 +1250,63 @@ bool OrganizerCore::waitForProcessCompletion(HANDLE handle, LPDWORD exitCode) DWORD res;
// Wait for a an event on the handle, a key press, mouse click or timeout
//TODO: Remove MOBase::isOneOf from this query as it was always returning true.
+
while (
- res = ::MsgWaitForMultipleObjects(1, &handle, false, 500,
- QS_KEY | QS_MOUSE),
- ((res != WAIT_FAILED) || (res != WAIT_OBJECT_0)) &&
- ((m_UserInterface == nullptr) || !m_UserInterface->unlockClicked())) {
-
- if (!::GetVFSProcessList(&numProcesses, processes)) {
- break;
- }
+ res = ::MsgWaitForMultipleObjects(1, &handle, false, 500,
+ QS_KEY | QS_MOUSE),
+ ((res != WAIT_FAILED) || (res != WAIT_OBJECT_0)) &&
+ ((m_UserInterface == nullptr) || !m_UserInterface->unlockClicked())) {
- bool found = false;
- size_t count =
- std::min<size_t>(static_cast<size_t>(maxCount), numProcesses);
- for (size_t i = 0; i < count; ++i) {
- std::wstring processName = getProcessName(processes[i]);
- if (!boost::starts_with(processName, L"ModOrganizer.exe")) {
- currentProcess = processes[i];
- m_UserInterface->setProcessName(QString::fromStdWString(processName));
- processHandle = ::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, currentProcess);
- found = true;
+ if (!::GetVFSProcessList(&numProcesses, processes)) {
+ break;
+ }
+ bool found = false;
+ size_t count =
+ std::min<size_t>(static_cast<size_t>(maxCount), numProcesses);
+ for (size_t i = 0; i < count; ++i) {
+ std::wstring processName = getProcessName(processes[i]);
+ if (!boost::starts_with(processName, L"ModOrganizer.exe")){
+ currentProcess = processes[i];
+ m_UserInterface->setProcessName(QString::fromStdWString(processName));
+ processHandle = ::OpenProcess(SYNCHRONIZE, FALSE, currentProcess);
+ found = true;
+ ::CloseHandle(processHandle);
+ }
}
- }
- if (!found) {
- // it's possible the previous process has deregistered before
- // the new one has registered, so we should try one more time
- // with a little delay
- if (tryAgain) {
- tryAgain = false;
- QThread::msleep(500);
- continue;
+ if (!found) {
+ // it's possible the previous process has deregistered before
+ // the new one has registered, so we should try one more time
+ // with a little delay
+ if (tryAgain) {
+ tryAgain = false;
+ QThread::msleep(500);
+ continue;
+ } else {
+ break;
+ }
} else {
- break;
+ tryAgain = true;
}
- } else {
- tryAgain = true;
- }
+ // keep processing events so the app doesn't appear dead
+
+ QCoreApplication::processEvents();
+
- // keep processing events so the app doesn't appear dead
- QCoreApplication::processEvents();
- }
+ if (exitCode != nullptr) {
+ //This is actually wrong if the process we started finished before we
+ //got the event and so we end up with a job handle.
+ if (! ::GetExitCodeProcess(processHandle, exitCode))
+ {
+ DWORD error = ::GetLastError();
+ qDebug() << "Failed to get process exit code: Error " << error;
+ }
+ }
- if (exitCode != nullptr) {
- //This is actually wrong if the process we started finished before we
- //got the event and so we end up with a job handle.
- if (! ::GetExitCodeProcess(processHandle, exitCode))
- {
- DWORD error = ::GetLastError();
- qDebug() << "Failed to get process exit code: Error " << error;
+ ::CloseHandle(processHandle);
+ if (handle != processHandle) {
+ ::CloseHandle(handle);
+ }
}
- }
-
- ::CloseHandle(processHandle);
- if (handle != processHandle) {
- ::CloseHandle(handle);
- }
-
return res == WAIT_OBJECT_0;
}
|
