Skip to content

Commit 723bab6

Browse files
committed
delay destruction of Menu if it's in nested loop
Fix nwjs#4697
1 parent 0cfb681 commit 723bab6

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

src/api/base/base.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,14 @@ class Base {
5757
std::string extension_id_;
5858
ObjectManager* object_manager() const { return object_manager_.get(); }
5959

60-
private:
60+
bool delay_destruction() { return delay_destruction_; }
61+
void set_delay_destruction(bool val) { delay_destruction_ = val; }
62+
bool pending_destruction() { return pending_destruction_; }
63+
void set_pending_destruction (bool val) { pending_destruction_ = val; }
64+
protected:
6165
int id_;
66+
bool delay_destruction_;
67+
bool pending_destruction_;
6268
base::WeakPtr<ObjectManager> object_manager_;
6369

6470
DISALLOW_COPY_AND_ASSIGN(Base);

src/api/menu/menu_views.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,17 @@ void Menu::Popup(int x, int y, content::RenderFrameHost* rfh) {
124124
screen_position_client->ConvertPointToScreen(target_window,
125125
&screen_point);
126126
}
127+
set_delay_destruction(true);
127128
views::MenuRunner runner(menu_model_.get(), views::MenuRunner::CONTEXT_MENU);
128-
if (views::MenuRunner::MENU_DELETED ==
129+
ignore_result(
129130
runner.RunMenuAt(top_level_widget,
130131
NULL,
131132
gfx::Rect(screen_point, gfx::Size()),
132133
views::MENU_ANCHOR_TOPRIGHT,
133-
ui::MENU_SOURCE_NONE))
134-
return;
135-
// menu_->RunMenuAt(screen_point, views::Menu2::ALIGN_TOPLEFT);
134+
ui::MENU_SOURCE_NONE));
135+
set_delay_destruction(false);
136+
if (pending_destruction())
137+
object_manager_->OnDeallocateObject(id_);
136138
}
137139

138140
void Menu::UpdateKeys(views::FocusManager *focus_manager){

src/api/nw_object.idl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ namespace nw.Obj {
1010
static void destroy(long id);
1111
static void callObjectMethod(long id, DOMString type, DOMString method, any[] arguments);
1212
static any callObjectMethodSync(long id, DOMString type, DOMString method, any[] arguments);
13-
static any callObjectMethodAsync(long id, DOMString type, DOMString method, any[] arguments);
13+
static void callObjectMethodAsync(long id, DOMString type, DOMString method, any[] arguments);
1414
};
1515
};

src/api/object_manager.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,14 @@ void ObjectManager::OnAllocateObject(int object_id,
120120

121121
void ObjectManager::OnDeallocateObject(int object_id) {
122122
DLOG(INFO) << "OnDeallocateObject: object_id:" << object_id;
123-
if (objects_registry_.Lookup(object_id))
123+
Base* obj = objects_registry_.Lookup(object_id);
124+
if (obj) {
125+
if (obj->delay_destruction()) {
126+
obj->set_pending_destruction(true);
127+
return;
128+
}
124129
objects_registry_.Remove(object_id);
130+
}
125131
objects_.erase(object_id);
126132
}
127133

0 commit comments

Comments
 (0)