diff --git a/shell/common/node_bindings.cc b/shell/common/node_bindings.cc index 84500938e5d2..ebc9725a96a4 100644 --- a/shell/common/node_bindings.cc +++ b/shell/common/node_bindings.cc @@ -336,9 +336,11 @@ node::Environment* NodeBindings::CreateEnvironment( args.insert(args.begin() + 1, init_script); std::unique_ptr c_argv = StringVectorToArgArray(args); - node::Environment* env = node::CreateEnvironment( - node::CreateIsolateData(context->GetIsolate(), uv_loop_, platform), - context, args.size(), c_argv.get(), 0, nullptr, bootstrap_env); + isolate_data_ = + node::CreateIsolateData(context->GetIsolate(), uv_loop_, platform); + node::Environment* env = + node::CreateEnvironment(isolate_data_, context, args.size(), c_argv.get(), + 0, nullptr, bootstrap_env); DCHECK(env); // Clean up the global _noBrowserGlobals that we unironically injected into diff --git a/shell/common/node_bindings.h b/shell/common/node_bindings.h index 97815722444e..6013f68ea337 100644 --- a/shell/common/node_bindings.h +++ b/shell/common/node_bindings.h @@ -19,6 +19,7 @@ class MessageLoop; namespace node { class Environment; class MultiIsolatePlatform; +class IsolateData; } // namespace node namespace electron { @@ -54,6 +55,8 @@ class NodeBindings { // Do message loop integration. virtual void RunMessageLoop(); + node::IsolateData* isolate_data() const { return isolate_data_; } + // Gets/sets the environment to wrap uv loop. void set_uv_env(node::Environment* env) { uv_env_ = env; } node::Environment* uv_env() const { return uv_env_; } @@ -106,6 +109,9 @@ class NodeBindings { // Environment that to wrap the uv loop. node::Environment* uv_env_ = nullptr; + // Isolate data used in creating the environment + node::IsolateData* isolate_data_ = nullptr; + base::WeakPtrFactory weak_factory_; DISALLOW_COPY_AND_ASSIGN(NodeBindings); diff --git a/shell/renderer/atom_renderer_client.cc b/shell/renderer/atom_renderer_client.cc index 523eb81bf7a9..3aef1c68d334 100644 --- a/shell/renderer/atom_renderer_client.cc +++ b/shell/renderer/atom_renderer_client.cc @@ -176,8 +176,12 @@ void AtomRendererClient::WillReleaseScriptContext( // avoid memory leaks auto* command_line = base::CommandLine::ForCurrentProcess(); if (command_line->HasSwitch(switches::kNodeIntegrationInSubFrames) || - command_line->HasSwitch(switches::kDisableElectronSiteInstanceOverrides)) + command_line->HasSwitch( + switches::kDisableElectronSiteInstanceOverrides)) { node::FreeEnvironment(env); + if (env == node_bindings_->uv_env()) + node::FreeIsolateData(node_bindings_->isolate_data()); + } // ElectronBindings is tracking node environments. electron_bindings_->EnvironmentDestroyed(env); diff --git a/shell/renderer/web_worker_observer.cc b/shell/renderer/web_worker_observer.cc index ffc9d2f8b667..7ba0a5749ed1 100644 --- a/shell/renderer/web_worker_observer.cc +++ b/shell/renderer/web_worker_observer.cc @@ -38,6 +38,7 @@ WebWorkerObserver::WebWorkerObserver() WebWorkerObserver::~WebWorkerObserver() { lazy_tls.Pointer()->Set(nullptr); node::FreeEnvironment(node_bindings_->uv_env()); + node::FreeIsolateData(node_bindings_->isolate_data()); asar::ClearArchives(); }