From fd52450d16a584aa72e44371adb1a8abb2e42e1e Mon Sep 17 00:00:00 2001 From: Rajendra Singh Date: Sun, 21 Oct 2018 12:56:48 +0530 Subject: [PATCH] Fixed memory leaks Close #4945 --- PowerEditor/src/MISC/Common/Common.cpp | 1 - PowerEditor/src/MISC/Exception/MiniDumper.cpp | 1 + .../src/MISC/PluginsManager/PluginsManager.cpp | 6 ++++++ .../WinControls/PluginsAdmin/pluginsAdmin.cpp | 18 +++++++++++++----- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/PowerEditor/src/MISC/Common/Common.cpp b/PowerEditor/src/MISC/Common/Common.cpp index eb097d97..99c79cdb 100644 --- a/PowerEditor/src/MISC/Common/Common.cpp +++ b/PowerEditor/src/MISC/Common/Common.cpp @@ -952,7 +952,6 @@ bool str2Clipboard(const generic_string &str2cpy, HWND hwnd) unsigned int clipBoardFormat = CF_UNICODETEXT; if (::SetClipboardData(clipBoardFormat, hglbCopy) == NULL) { - ::GlobalUnlock(hglbCopy); ::GlobalFree(hglbCopy); ::CloseClipboard(); return false; diff --git a/PowerEditor/src/MISC/Exception/MiniDumper.cpp b/PowerEditor/src/MISC/Exception/MiniDumper.cpp index 70ce5eb8..7ef2cbe6 100644 --- a/PowerEditor/src/MISC/Exception/MiniDumper.cpp +++ b/PowerEditor/src/MISC/Exception/MiniDumper.cpp @@ -98,6 +98,7 @@ bool MiniDumper::writeDump(EXCEPTION_POINTERS * pExceptionInfo) { szResult = TEXT("The debugging DLL is outdated,\r\nfind a recent copy of dbghelp.dll and install it."); } + ::FreeLibrary(hDll); } else { diff --git a/PowerEditor/src/MISC/PluginsManager/PluginsManager.cpp b/PowerEditor/src/MISC/PluginsManager/PluginsManager.cpp index 04150356..508821b3 100644 --- a/PowerEditor/src/MISC/PluginsManager/PluginsManager.cpp +++ b/PowerEditor/src/MISC/PluginsManager/PluginsManager.cpp @@ -345,7 +345,13 @@ bool PluginsManager::loadPluginsV2(const TCHAR* dir) generic_string dllName2 = foundData.cFileName; dllName2 += TEXT(".dll"); PathAppend(pluginsFullPathFilter2, dllName2); + // get plugin + if (hFindDll) + { + ::FindClose(hFindDll); + hFindDll = INVALID_HANDLE_VALUE; + } hFindDll = ::FindFirstFile(pluginsFullPathFilter2.c_str(), &foundData); if (hFindDll != INVALID_HANDLE_VALUE && !(foundData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { diff --git a/PowerEditor/src/WinControls/PluginsAdmin/pluginsAdmin.cpp b/PowerEditor/src/WinControls/PluginsAdmin/pluginsAdmin.cpp index 3a1cf51b..fb203eeb 100644 --- a/PowerEditor/src/WinControls/PluginsAdmin/pluginsAdmin.cpp +++ b/PowerEditor/src/WinControls/PluginsAdmin/pluginsAdmin.cpp @@ -79,7 +79,7 @@ void Version::setVersionFrom(generic_string filePath) { if (not filePath.empty() && ::PathFileExists(filePath.c_str())) { - DWORD handle; + DWORD handle = 0; DWORD bufferSize = ::GetFileVersionInfoSize(filePath.c_str(), &handle); if (bufferSize <= 0) @@ -88,9 +88,9 @@ void Version::setVersionFrom(generic_string filePath) unsigned char* buffer = new unsigned char[bufferSize]; ::GetFileVersionInfo(filePath.c_str(), handle, bufferSize, buffer); - VS_FIXEDFILEINFO* lpFileInfo; + VS_FIXEDFILEINFO* lpFileInfo = nullptr; UINT cbFileInfo = 0; - VerQueryValue(buffer, TEXT("\\"), (LPVOID*)&lpFileInfo, &cbFileInfo); + VerQueryValue(buffer, TEXT("\\"), reinterpret_cast(&lpFileInfo), &cbFileInfo); if (cbFileInfo) { _major = (lpFileInfo->dwFileVersionMS & 0xFFFF0000) >> 16; @@ -98,6 +98,7 @@ void Version::setVersionFrom(generic_string filePath) _patch = (lpFileInfo->dwFileVersionLS & 0xFFFF0000) >> 16; _build = lpFileInfo->dwFileVersionLS & 0x0000FFFF; } + delete[] buffer; } } @@ -714,7 +715,10 @@ bool PluginsAdminDlg::isValide() bool PluginsAdminDlg::updateListAndLoadFromJson() { - try { + HMODULE hLib = NULL; + + try + { if (!isValide()) return false; @@ -728,7 +732,7 @@ bool PluginsAdminDlg::updateListAndLoadFromJson() #else //RELEASE - HINSTANCE hLib = ::LoadLibrary(_pluginListFullPath.c_str()); + hLib = ::LoadLibrary(_pluginListFullPath.c_str()); if (!hLib) { // Error treatment @@ -741,6 +745,7 @@ bool PluginsAdminDlg::updateListAndLoadFromJson() { // Error treatment //printStr(TEXT("getList PB!!!")); + ::FreeLibrary(hLib); return false; } @@ -770,11 +775,14 @@ bool PluginsAdminDlg::updateListAndLoadFromJson() // initialize installed list view loadFromPluginInfos(); + ::FreeLibrary(hLib); return true; } catch (...) { // whichever exception + if (hLib) + ::FreeLibrary(hLib); return false; } }