From 50cc54e50b58f7b2a39119be4a92c2d300f28ab8 Mon Sep 17 00:00:00 2001 From: CezaryKulakowski <50166166+CezaryKulakowski@users.noreply.github.com> Date: Tue, 6 Aug 2019 23:55:00 +0200 Subject: [PATCH] fix: keep references to active menus created by api Menu (#19427) Without this such menus would be destroyed by js garbage collector even when they are still displayed. --- shell/browser/api/atom_api_menu.cc | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/shell/browser/api/atom_api_menu.cc b/shell/browser/api/atom_api_menu.cc index 48a06d5b057b..cb4e94316179 100644 --- a/shell/browser/api/atom_api_menu.cc +++ b/shell/browser/api/atom_api_menu.cc @@ -4,6 +4,8 @@ #include "shell/browser/api/atom_api_menu.h" +#include + #include "native_mate/constructor.h" #include "native_mate/dictionary.h" #include "native_mate/object_template_builder.h" @@ -14,6 +16,13 @@ #include "shell/common/native_mate_converters/string16_converter.h" #include "shell/common/node_includes.h" +namespace { +// We need this map to keep references to currently opened menus. +// Without this menus would be destroyed by js garbage collector +// even when they are still displayed. +std::map> g_menus; +} // unnamed namespace + namespace electron { namespace api { @@ -200,10 +209,12 @@ bool Menu::WorksWhenHiddenAt(int index) const { } void Menu::OnMenuWillClose() { + g_menus.erase(weak_map_id()); Emit("menu-will-close"); } void Menu::OnMenuWillShow() { + g_menus[weak_map_id()] = v8::Global(isolate(), GetWrapper()); Emit("menu-will-show"); }