Skip to content

Commit 6dbaafb

Browse files
committed
DispatcherHost owns API objects and deletion
The Window object should be deleted on browser side when DispatcherHost is removed (e.g. dev reload), which will then trigger decoupling with shell, or it cannot be closed. Fix nwjs#1330
1 parent f779a42 commit 6dbaafb

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/api/dispatcher_host.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ DispatcherHost::DispatcherHost(content::RenderViewHost* render_view_host)
5757

5858
DispatcherHost::~DispatcherHost() {
5959
g_dispatcher_host_map.erase(render_view_host());
60+
std::set<int>::iterator it;
61+
for (it = objects_.begin(); it != objects_.end(); it++) {
62+
if (Base* obj = GetApiObject(*it)) {
63+
delete obj;
64+
}
65+
}
6066
}
6167

6268
DispatcherHost*
@@ -138,11 +144,14 @@ void DispatcherHost::OnAllocateObject(int object_id,
138144
LOG(ERROR) << "Allocate an object of unknown type: " << type;
139145
objects_registry_.AddWithID(new Base(object_id, weak_ptr_factory_.GetWeakPtr(), option), object_id);
140146
}
147+
objects_.insert(object_id);
141148
}
142149

143150
void DispatcherHost::OnDeallocateObject(int object_id) {
144151
DLOG(INFO) << "OnDeallocateObject: object_id:" << object_id;
145-
objects_registry_.Remove(object_id);
152+
if (objects_registry_.Lookup(object_id))
153+
objects_registry_.Remove(object_id);
154+
objects_.erase(object_id);
146155
}
147156

148157
void DispatcherHost::OnCallObjectMethod(

src/api/dispatcher_host.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "content/public/browser/render_view_host_observer.h"
2828

2929
#include <string>
30+
#include <set>
3031

3132
namespace base {
3233
class DictionaryValue;
@@ -78,6 +79,8 @@ class DispatcherHost : public content::RenderViewHostObserver {
7879
static IDMap<Base, IDMapOwnPointer> objects_registry_;
7980
static int next_object_id_;
8081

82+
std::set<int> objects_;
83+
8184
// Factory to generate weak pointer
8285
base::WeakPtrFactory<DispatcherHost> weak_ptr_factory_;
8386

0 commit comments

Comments
 (0)