Skip to content

Commit

Permalink
fix: memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
akiver committed Jan 28, 2022
1 parent 3229402 commit 1e9ec4f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
10 changes: 5 additions & 5 deletions WindowFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ struct EnumWindowsCallbackArgs {
vector<HWND> windowHandles;
};

LPWSTR GetProcessNameFromProcessId(const DWORD processId) {
LPWSTR filename = new WCHAR[MAX_PATH];
wstring GetProcessNameFromProcessId(const DWORD processId) {
wchar_t filename[MAX_PATH] = {0};
HANDLE processHandle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processId);

if (processHandle != nullptr) {
GetModuleFileNameEx(processHandle, NULL, filename, MAX_PATH);
CloseHandle(processHandle);
}

return filename;
return wstring(filename);
}

BOOL WindowFinder::IsWindowOnCurrentDesktop(HWND windowHandle) {
Expand All @@ -39,9 +39,9 @@ BOOL WindowFinder::IsWindowOnCurrentDesktop(HWND windowHandle) {
BOOL WindowFinder::IsWindowFromCurrentProcess(HWND windowHandle) {
DWORD windowProcessId;
GetWindowThreadProcessId(windowHandle, &windowProcessId);
LPWSTR processName = GetProcessNameFromProcessId(windowProcessId);
wstring processName = GetProcessNameFromProcessId(windowProcessId);

return wcscmp(currentProcessName, processName) == 0;
return currentProcessName == processName;
}

BOOL CALLBACK EnumWindowsCallback(HWND windowHandle, LPARAM parameters) {
Expand Down
3 changes: 2 additions & 1 deletion WindowFinder.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <shlobj.h>
#include <string>
#include <vector>

class WindowFinder {
Expand All @@ -9,7 +10,7 @@ class WindowFinder {
BOOL IsWindowOnCurrentDesktop(HWND windowHandle);
BOOL IsWindowFromCurrentProcess(HWND windowHandle);
HWND currentWindowHandle;
LPWSTR currentProcessName;
std::wstring currentProcessName;

private:
IVirtualDesktopManager *desktopManager; // Win10 ++ only.
Expand Down

0 comments on commit 1e9ec4f

Please sign in to comment.