diff --git a/lib/browser/objects-registry.js b/lib/browser/objects-registry.js index aeaf5351dc0..048fe856a2c 100644 --- a/lib/browser/objects-registry.js +++ b/lib/browser/objects-registry.js @@ -19,17 +19,14 @@ class ObjectsRegistry { // registered then the already assigned ID would be returned. add (webContents, obj) { // Get or assign an ID to the object. - let id = this.saveToStorage(obj) + const id = this.saveToStorage(obj) // Add object to the set of referenced objects. - let webContentsId = webContents.getId() + const webContentsId = webContents.getId() let owner = this.owners[webContentsId] if (!owner) { owner = this.owners[webContentsId] = new Set() - // Clear the storage when webContents is reloaded/navigated. - webContents.once('render-view-deleted', () => { - this.clear(webContentsId) - }) + this.registerDeleteListener(webContents, webContentsId) } if (!owner.has(id)) { owner.add(id) @@ -39,6 +36,18 @@ class ObjectsRegistry { return id } + // Clear the storage when webContents is reloaded/navigated. + registerDeleteListener (webContents, webContentsId) { + const processId = webContents.getProcessId() + const listener = (event, deletedProcessId) => { + if (deletedProcessId === processId) { + webContents.removeListener('render-view-deleted', listener) + this.clear(webContentsId) + } + } + webContents.on('render-view-deleted', listener) + } + // Get an object according to its ID. get (id) { const pointer = this.storage[id] @@ -90,7 +99,7 @@ class ObjectsRegistry { pointer.count -= 1 if (pointer.count === 0) { v8Util.deleteHiddenValue(pointer.object, 'atomId') - return delete this.storage[id] + delete this.storage[id] } } }