From b457e5103c2b8e486d9b5864a8e9ea23e9e9cf0d Mon Sep 17 00:00:00 2001 From: Martin Fuchs Date: Sat, 23 Aug 2003 10:02:38 +0000 Subject: [PATCH] fixed more memory and GDI handle leaks svn path=/trunk/; revision=5791 --- reactos/subsys/system/explorer/shell/pane.cpp | 16 +++++----------- .../system/explorer/taskbar/quicklaunch.cpp | 6 ++---- .../system/explorer/taskbar/startmenu.cpp | 12 +++++------- .../subsys/system/explorer/taskbar/startmenu.h | 11 +++++++++-- .../subsys/system/explorer/taskbar/taskbar.cpp | 4 +--- .../subsys/system/explorer/utility/utility.h | 17 ++++++++++++++--- 6 files changed, 36 insertions(+), 30 deletions(-) diff --git a/reactos/subsys/system/explorer/shell/pane.cpp b/reactos/subsys/system/explorer/shell/pane.cpp index 23ab6ddd0cd..71f35d161f2 100644 --- a/reactos/subsys/system/explorer/shell/pane.cpp +++ b/reactos/subsys/system/explorer/shell/pane.cpp @@ -841,28 +841,22 @@ int Pane::Notify(int id, NMHDR* pnmh) OutputWorker::OutputWorker() { - HDC hdc = GetDC(0); - - _hfont = CreateFont(-MulDiv(8,GetDeviceCaps(hdc,LOGPIXELSY),72), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, TEXT("MS Sans Serif")); - - ReleaseDC(0, hdc); + _hfont = CreateFont(-MulDiv(8,GetDeviceCaps(WindowCanvas(0),LOGPIXELSY),72), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, TEXT("MS Sans Serif")); } void OutputWorker::init_output(HWND hwnd) { TCHAR b[16]; - HFONT old_font; - HDC hdc = GetDC(hwnd); + + WindowCanvas canvas(hwnd); if (GetNumberFormat(LOCALE_USER_DEFAULT, 0, TEXT("1000"), 0, b, 16) > 4) _num_sep = b[1]; else _num_sep = TEXT('.'); - old_font = SelectFont(hdc, _hfont); - GetTextExtentPoint32(hdc, TEXT(" "), 1, &_spaceSize); - SelectFont(hdc, old_font); - ReleaseDC(hwnd, hdc); + FontSelection font(canvas, _hfont); + GetTextExtentPoint32(canvas, TEXT(" "), 1, &_spaceSize); } diff --git a/reactos/subsys/system/explorer/taskbar/quicklaunch.cpp b/reactos/subsys/system/explorer/taskbar/quicklaunch.cpp index 836d5c9d4d3..525f56a7c88 100644 --- a/reactos/subsys/system/explorer/taskbar/quicklaunch.cpp +++ b/reactos/subsys/system/explorer/taskbar/quicklaunch.cpp @@ -96,7 +96,7 @@ void QuickLaunchBar::AddShortcuts() _dir->smart_scan(); ShellFolder desktop_folder; - HDC hdc_wnd = GetDC(_hwnd); + WindowCanvas canvas(_hwnd); TBBUTTON btn = {-2/*I_IMAGENONE*/, 0, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0}; @@ -110,7 +110,7 @@ void QuickLaunchBar::AddShortcuts() ShellEntry* shell_entry = static_cast(entry); const String& entry_name = desktop_folder.get_name(shell_entry->_pidl); - HBITMAP hbmp = create_bitmap_from_icon(shell_entry->_hIcon, GetSysColorBrush(COLOR_BTNFACE), hdc_wnd); + HBITMAP hbmp = create_bitmap_from_icon(shell_entry->_hIcon, GetSysColorBrush(COLOR_BTNFACE), canvas); TBADDBITMAP ab = {0, (UINT_PTR)hbmp}; int bmp_idx = SendMessage(_hwnd, TB_ADDBITMAP, 1, (LPARAM)&ab); @@ -132,8 +132,6 @@ void QuickLaunchBar::AddShortcuts() SendMessage(_hwnd, TB_INSERTBUTTON, idx, (LPARAM)&btn); } } - - ReleaseDC(_hwnd, hdc_wnd); } LRESULT QuickLaunchBar::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam) diff --git a/reactos/subsys/system/explorer/taskbar/startmenu.cpp b/reactos/subsys/system/explorer/taskbar/startmenu.cpp index a638b0aca8b..2538eed0c52 100644 --- a/reactos/subsys/system/explorer/taskbar/startmenu.cpp +++ b/reactos/subsys/system/explorer/taskbar/startmenu.cpp @@ -304,7 +304,7 @@ void StartMenu::AddButton(LPCTSTR title, HICON hIcon, bool hasSubmenu, UINT id, } // widen window, if it is too small - int width = StartMenuButton::GetTextWidth(title) + 16/*icon*/ + 10/*placeholder*/ + 16/*arrow*/; + int width = StartMenuButton::GetTextWidth(title,_hwnd) + 16/*icon*/ + 10/*placeholder*/ + 16/*arrow*/; ClientRect clnt(_hwnd); if (width > clnt.right) @@ -436,15 +436,13 @@ void StartMenu::CloseStartMenu(int id) } -int StartMenuButton::GetTextWidth(LPCTSTR title) +int StartMenuButton::GetTextWidth(LPCTSTR title, HWND hwnd) { - HDC hdc = GetDC(0); - FontSelection font(hdc, GetStockFont(DEFAULT_GUI_FONT)); + WindowCanvas canvas(hwnd); + FontSelection font(canvas, GetStockFont(DEFAULT_GUI_FONT)); RECT rect = {0, 0, 0, 0}; - DrawText(hdc, title, -1, &rect, DT_SINGLELINE|DT_NOPREFIX|DT_CALCRECT); - - ReleaseDC(0, hdc); + DrawText(canvas, title, -1, &rect, DT_SINGLELINE|DT_NOPREFIX|DT_CALCRECT); return rect.right-rect.left; } diff --git a/reactos/subsys/system/explorer/taskbar/startmenu.h b/reactos/subsys/system/explorer/taskbar/startmenu.h index bd5ceaa51d8..5f531ad8e91 100644 --- a/reactos/subsys/system/explorer/taskbar/startmenu.h +++ b/reactos/subsys/system/explorer/taskbar/startmenu.h @@ -44,7 +44,14 @@ struct StartMenuDirectory { StartMenuDirectory(const ShellDirectory& dir, bool subfolders=true) - : _dir(dir), _subfolders(subfolders) {} + : _dir(dir), _subfolders(subfolders) + { + } + + ~StartMenuDirectory() + { + _dir.free_subentries(); + } ShellDirectory _dir; bool _subfolders; @@ -72,7 +79,7 @@ struct StartMenuButton : public OwnerdrawnButton StartMenuButton(HWND hwnd, HICON hIcon, bool hasSubmenu) : super(hwnd), _hIcon(hIcon), _hasSubmenu(hasSubmenu) {} - static int GetTextWidth(LPCTSTR title); + static int GetTextWidth(LPCTSTR title, HWND hwnd); protected: LRESULT WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam); diff --git a/reactos/subsys/system/explorer/taskbar/taskbar.cpp b/reactos/subsys/system/explorer/taskbar/taskbar.cpp index 9f3496c8fe4..6c265d5b896 100644 --- a/reactos/subsys/system/explorer/taskbar/taskbar.cpp +++ b/reactos/subsys/system/explorer/taskbar/taskbar.cpp @@ -236,9 +236,7 @@ BOOL CALLBACK TaskBar::EnumWndProc(HWND hwnd, LPARAM lparam) found->second._id = pThis->_next_id++; } else { HICON hIcon = get_window_icon(hwnd); - HDC hdc_wnd = GetDC(pThis->_htoolbar); - HBITMAP hbmp = create_bitmap_from_icon(hIcon, GetSysColorBrush(COLOR_BTNFACE), hdc_wnd); - ReleaseDC(pThis->_htoolbar, hdc_wnd); + HBITMAP hbmp = create_bitmap_from_icon(hIcon, GetSysColorBrush(COLOR_BTNFACE), WindowCanvas(pThis->_htoolbar)); TBADDBITMAP ab = {0, (UINT_PTR)hbmp}; int bmp_idx = SendMessage(pThis->_htoolbar, TB_ADDBITMAP, 1, (LPARAM)&ab); diff --git a/reactos/subsys/system/explorer/utility/utility.h b/reactos/subsys/system/explorer/utility/utility.h index c19c9259169..3570c317b4f 100644 --- a/reactos/subsys/system/explorer/utility/utility.h +++ b/reactos/subsys/system/explorer/utility/utility.h @@ -186,9 +186,6 @@ protected: HWND _hwnd; }; - - // double buffering classes - struct Canvas { Canvas(HDC hdc) : _hdc(hdc) {} @@ -199,6 +196,20 @@ protected: HDC _hdc; }; +struct WindowCanvas : public Canvas +{ + WindowCanvas(HWND hwnd) + : Canvas(GetDC(hwnd)), _hwnd(hwnd) {} + + ~WindowCanvas() {ReleaseDC(_hwnd, _hdc);} + +protected: + HWND _hwnd; +}; + + + // double buffering classes + struct MemCanvas : public Canvas { MemCanvas(HDC hdc=0)