From d9d4c989543eca8b48ee28284df37da5e7358756 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 Without this such menus would be destroyed by js garbage collector even when they are still displayed. --- shell/browser/api/atom_api_menu.cc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/shell/browser/api/atom_api_menu.cc b/shell/browser/api/atom_api_menu.cc index 5acdc0f0edde2..8e52ad8e7f45a 100644 --- a/shell/browser/api/atom_api_menu.cc +++ b/shell/browser/api/atom_api_menu.cc @@ -4,6 +4,7 @@ #include "shell/browser/api/atom_api_menu.h" +#include #include #include "native_mate/constructor.h" @@ -16,6 +17,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 { @@ -202,10 +210,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"); }