From 7a3bc5505f60fa16a34aac4fb209dbb4968db9bf Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Tue, 1 Aug 2023 19:43:04 +0300 Subject: [PATCH] fix(windows): don't drop `Menu` or `MenuChild` inside menu proc (#81) `Menu` or `MenuChild` are not owned by the menu proc and so it shouldn't drop it, previously this was okay because the `Menu` or `MenuChild` used to be a wrapper around `Rc` but that was changed in the last release. --- .changes/destroy.md | 5 +++++ src/platform_impl/windows/mod.rs | 14 +------------- 2 files changed, 6 insertions(+), 13 deletions(-) create mode 100644 .changes/destroy.md diff --git a/.changes/destroy.md b/.changes/destroy.md new file mode 100644 index 0000000..7fe4b4b --- /dev/null +++ b/.changes/destroy.md @@ -0,0 +1,5 @@ +--- +"muda": "patch" +--- + +On Windows, fix `ContextMenu::detach_menu_subclass_from_hwnd` crashing and terminating the thread. diff --git a/src/platform_impl/windows/mod.rs b/src/platform_impl/windows/mod.rs index f8e3901..211ed73 100644 --- a/src/platform_impl/windows/mod.rs +++ b/src/platform_impl/windows/mod.rs @@ -36,7 +36,7 @@ use windows_sys::Win32::{ MFS_CHECKED, MFS_DISABLED, MF_BYCOMMAND, MF_BYPOSITION, MF_CHECKED, MF_DISABLED, MF_ENABLED, MF_GRAYED, MF_POPUP, MF_SEPARATOR, MF_STRING, MF_UNCHECKED, MIIM_BITMAP, MIIM_STATE, MIIM_STRING, SW_HIDE, SW_MAXIMIZE, SW_MINIMIZE, TPM_LEFTALIGN, WM_CLOSE, - WM_COMMAND, WM_DESTROY, + WM_COMMAND, }, }, }; @@ -298,7 +298,6 @@ impl Menu { .ok_or(crate::Error::NotInitialized)?; self.hwnds.remove(index); unsafe { - SendMessageW(hwnd, WM_CLEAR_MENU_DATA, 0, 0); RemoveWindowSubclass(hwnd, Some(menu_subclass_proc), MENU_SUBCLASS_ID); SetMenu(hwnd, 0); DrawMenuBar(hwnd); @@ -320,7 +319,6 @@ impl Menu { pub fn detach_menu_subclass_from_hwnd(&self, hwnd: isize) { unsafe { - SendMessageW(hwnd, WM_CLEAR_MENU_DATA, 0, 0); RemoveWindowSubclass(hwnd, Some(menu_subclass_proc), MENU_SUBCLASS_ID); } } @@ -799,7 +797,6 @@ impl MenuChild { pub fn detach_menu_subclass_from_hwnd(&self, hwnd: isize) { unsafe { - SendMessageW(hwnd, WM_CLEAR_MENU_DATA, 0, 0); RemoveWindowSubclass(hwnd, Some(menu_subclass_proc), SUBMENU_SUBCLASS_ID); } } @@ -898,7 +895,6 @@ fn create_icon_item_info(hbitmap: HBITMAP) -> MENUITEMINFOW { const MENU_SUBCLASS_ID: usize = 200; const SUBMENU_SUBCLASS_ID: usize = 201; -const WM_CLEAR_MENU_DATA: u32 = 600; unsafe extern "system" fn menu_subclass_proc( hwnd: HWND, @@ -909,14 +905,6 @@ unsafe extern "system" fn menu_subclass_proc( dwrefdata: usize, ) -> LRESULT { let mut ret = -1; - if msg == WM_DESTROY || msg == WM_CLEAR_MENU_DATA { - if uidsubclass == MENU_SUBCLASS_ID { - drop(Box::from_raw(dwrefdata as *mut Menu)); - } else { - drop(Box::from_raw(dwrefdata as *mut MenuChild)); - } - } - if msg == WM_COMMAND { let id = util::LOWORD(wparam as _) as u32; let item = if uidsubclass == MENU_SUBCLASS_ID {