Skip to content

Commit 8114494

Browse files
committed
Fix double-deletion risk in tray icon context menu
Replaces creation of owning shared_ptr for Menu with a non-owning aliasing shared_ptr using an empty deleter. This prevents double-deletion since Menu objects are managed elsewhere and should not have a separate control block.
1 parent 74b130d commit 8114494

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/capi/tray_icon_c.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,13 @@ void native_tray_icon_set_context_menu(native_tray_icon_t tray_icon, native_menu
142142
try {
143143
auto tray_icon_ptr = static_cast<TrayIcon*>(tray_icon);
144144
if (menu) {
145-
auto menu_ptr = std::shared_ptr<Menu>(static_cast<Menu*>(menu));
145+
// IMPORTANT: Do NOT create an owning shared_ptr from a raw pointer here.
146+
// Menu objects are owned by the global registry in menu_c.cpp and by Swift-side
147+
// references. Creating an owning shared_ptr would introduce a separate control
148+
// block and cause double-deletion during shutdown.
149+
// Use a non-owning aliasing shared_ptr with an empty deleter instead.
150+
auto menu_raw = static_cast<Menu*>(menu);
151+
auto menu_ptr = std::shared_ptr<Menu>(menu_raw, [](Menu*){});
146152
tray_icon_ptr->SetContextMenu(menu_ptr);
147153
} else {
148154
tray_icon_ptr->SetContextMenu(nullptr);

0 commit comments

Comments
 (0)