mirror of
https://foundry.openuru.org/gitblit/r/CWE-ou-minkata.git
synced 2025-07-18 11:19:10 +00:00
Handle smart pointers...
This fixes a few HANDLE leaks. They aren't a big deal in the grand scheme of things, but it's good to be correct.
This commit is contained in:
@ -66,26 +66,29 @@ static plClientLauncher s_launcher;
|
||||
static UINT s_taskbarCreated = RegisterWindowMessageW(L"TaskbarButtonCreated");
|
||||
static ITaskbarList3* s_taskbar = nullptr;
|
||||
|
||||
typedef std::unique_ptr<void, std::function<BOOL(HANDLE)>> handleptr_t;
|
||||
|
||||
// ===================================================
|
||||
|
||||
/** Create a global patcher mutex that is backwards compatible with eap's */
|
||||
static HANDLE CreatePatcherMutex()
|
||||
static handleptr_t CreatePatcherMutex()
|
||||
{
|
||||
return CreateMutexW(nullptr, FALSE, plManifest::PatcherExecutable().AsString().ToWchar());
|
||||
return handleptr_t(CreateMutexW(nullptr, TRUE, plManifest::PatcherExecutable().AsString().ToWchar()),
|
||||
CloseHandle);
|
||||
}
|
||||
|
||||
static bool IsPatcherRunning()
|
||||
{
|
||||
HANDLE mut = CreatePatcherMutex();
|
||||
return WaitForSingleObject(mut, 0) != WAIT_OBJECT_0;
|
||||
handleptr_t mut = CreatePatcherMutex();
|
||||
return WaitForSingleObject(mut.get(), 0) != WAIT_OBJECT_0;
|
||||
}
|
||||
|
||||
static void WaitForOldPatcher()
|
||||
{
|
||||
HANDLE mut = CreatePatcherMutex();
|
||||
DWORD wait = WaitForSingleObject(mut, 0);
|
||||
handleptr_t mut = CreatePatcherMutex();
|
||||
DWORD wait = WaitForSingleObject(mut.get(), 0);
|
||||
while (wait != WAIT_OBJECT_0) // :( :( :(
|
||||
wait = WaitForSingleObject(mut, 100);
|
||||
wait = WaitForSingleObject(mut.get(), 100);
|
||||
Sleep(1000); // :(
|
||||
}
|
||||
|
||||
@ -231,7 +234,7 @@ static void ISetDownloadStatus(const plString& status)
|
||||
}
|
||||
|
||||
|
||||
static HANDLE ICreateProcess(const plFileName& exe, const plString& args)
|
||||
static handleptr_t ICreateProcess(const plFileName& exe, const plString& args)
|
||||
{
|
||||
STARTUPINFOW si;
|
||||
PROCESS_INFORMATION pi;
|
||||
@ -262,7 +265,7 @@ static HANDLE ICreateProcess(const plFileName& exe, const plString& args)
|
||||
// So maybe it needs elevation... Or maybe everything arseploded.
|
||||
if (result != FALSE) {
|
||||
CloseHandle(pi.hThread);
|
||||
return pi.hProcess;
|
||||
return handleptr_t(pi.hProcess, CloseHandle);
|
||||
} else if (GetLastError() == ERROR_ELEVATION_REQUIRED) {
|
||||
SHELLEXECUTEINFOW info;
|
||||
memset(&info, 0, sizeof(info));
|
||||
@ -272,7 +275,7 @@ static HANDLE ICreateProcess(const plFileName& exe, const plString& args)
|
||||
info.lpParameters = args.ToWchar();
|
||||
ShellExecuteExW(&info);
|
||||
|
||||
return info.hProcess;
|
||||
return handleptr_t(info.hProcess, CloseHandle);
|
||||
} else {
|
||||
wchar_t* msg = nullptr;
|
||||
FormatMessageW(
|
||||
@ -306,14 +309,13 @@ static bool IInstallRedist(const plFileName& exe)
|
||||
ss << " /norestart"; // I don't want to image the accusations of viruses and hacking if this happened...
|
||||
|
||||
// Now fire up the process...
|
||||
HANDLE process = ICreateProcess(exe, ss.GetString());
|
||||
handleptr_t process = ICreateProcess(exe, ss.GetString());
|
||||
if (process) {
|
||||
WaitForSingleObject(process, INFINITE);
|
||||
WaitForSingleObject(process.get(), INFINITE);
|
||||
|
||||
// Get the exit code so we can indicate success/failure to the redist thread
|
||||
DWORD code = PLASMA_OK;
|
||||
GetExitCodeProcess(process, &code);
|
||||
CloseHandle(process);
|
||||
GetExitCodeProcess(process.get(), &code);
|
||||
|
||||
return code != PLASMA_PHAILURE;
|
||||
}
|
||||
@ -329,17 +331,14 @@ static void ILaunchClientExecutable(const plFileName& exe, const plString& args)
|
||||
// Only launch a client executable if we're given one. If not, that's probably a cue that we're
|
||||
// done with some service operation and need to go away.
|
||||
if (!exe.AsString().IsEmpty()) {
|
||||
HANDLE hEvent = CreateEventW(nullptr, TRUE, FALSE, L"UruPatcherEvent");
|
||||
HANDLE process = ICreateProcess(exe, args);
|
||||
handleptr_t hEvent = handleptr_t(CreateEventW(nullptr, TRUE, FALSE, L"UruPatcherEvent"), CloseHandle);
|
||||
handleptr_t process = ICreateProcess(exe, args);
|
||||
|
||||
// if this is the real game client, then we need to make sure it gets this event...
|
||||
if (plManifest::ClientExecutable().AsString().CompareI(exe.AsString()) == 0) {
|
||||
WaitForInputIdle(process, 1000);
|
||||
WaitForSingleObject(hEvent, INFINITE);
|
||||
WaitForInputIdle(process.get(), 1000);
|
||||
WaitForSingleObject(hEvent.get(), INFINITE);
|
||||
}
|
||||
|
||||
CloseHandle(process);
|
||||
CloseHandle(hEvent);
|
||||
}
|
||||
|
||||
// time to hara-kiri...
|
||||
@ -399,7 +398,7 @@ int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdL
|
||||
IShowErrorDialog(text.ToWchar());
|
||||
return PLASMA_OK;
|
||||
}
|
||||
HANDLE _onePatcherMut = CreatePatcherMutex();
|
||||
HANDLE _onePatcherMut = CreatePatcherMutex().release();
|
||||
|
||||
// Initialize the network core
|
||||
s_launcher.InitializeNetCore();
|
||||
|
Reference in New Issue
Block a user