Skip to content

Commit cb9b3b2

Browse files
fix: keep references to active menus created by api Menu (electron#22151)
Without this such menus would be destroyed by js garbage collector even when they are still displayed. Co-authored-by: CezaryKulakowski <50166166+CezaryKulakowski@users.noreply.github.com>
1 parent da65c88 commit cb9b3b2

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

shell/browser/api/atom_api_menu.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "shell/browser/api/atom_api_menu.h"
66

7+
#include <map>
78
#include <utility>
89

910
#include "native_mate/constructor.h"
@@ -16,6 +17,13 @@
1617
#include "shell/common/native_mate_converters/string16_converter.h"
1718
#include "shell/common/node_includes.h"
1819

20+
namespace {
21+
// We need this map to keep references to currently opened menus.
22+
// Without this menus would be destroyed by js garbage collector
23+
// even when they are still displayed.
24+
std::map<uint32_t, v8::Global<v8::Object>> g_menus;
25+
} // unnamed namespace
26+
1927
namespace electron {
2028

2129
namespace api {
@@ -202,10 +210,12 @@ bool Menu::WorksWhenHiddenAt(int index) const {
202210
}
203211

204212
void Menu::OnMenuWillClose() {
213+
g_menus.erase(weak_map_id());
205214
Emit("menu-will-close");
206215
}
207216

208217
void Menu::OnMenuWillShow() {
218+
g_menus[weak_map_id()] = v8::Global<v8::Object>(isolate(), GetWrapper());
209219
Emit("menu-will-show");
210220
}
211221

0 commit comments

Comments
 (0)