如果渲染器进程关闭,则会收集电子全局变量垃圾? [英] Electron global variable garbage collected if renderer process closes?

查看:59
本文介绍了如果渲染器进程关闭,则会收集电子全局变量垃圾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Electron中,我的主要过程是打开BrowserWindow。 BrowserWindow加载一个html页面,然后同一窗口最终加载另一个html页面。

In Electron, I have my main process opening a BrowserWindow. The BrowserWindow loads one html page and then the same window eventually loads another html page.

main.js

var mainWindow;
global.mainState = {
  settings: {}
}
mainWindow = createWindow('main', {
  width: 1000,
  height: 800,
});
if (curState == 'load') {
  mainWindow.loadURL(`file://${__dirname}/interface/load.html`, {})
}
if (curState == 'login') {
  mainWindow.loadURL(`file://${__dirname}/interface/login.html`, {})
}

load.html

load.html

const remote = require('electron').remote;
var testGlobal = remote.getGlobal('mainState')
testGlobal.settings = 'test value'
testGlobal.settings.inner = 'test value2'

当main.js加载第二页(login.html)时,是否会删除/取消引用全局变量?文档说,如果渲染器进程取消引用了全局变量,则该变量将被gc标记。当我尝试对此进行测试时,我会得出不一致的结果,我只想从某个人那里获得比我更明智的解释。

When main.js loads the second page (login.html), will the global variable be deleted/dereferenced? The docs say that if the renderer process dereferences a global variable then the variable will be gc'd. When I try to test this I get inconsistent results and I would just like some explanation from someone more wise than I.

推荐答案

<由于站点更改,将对code> testGlobal 进行垃圾收集。 global.mainState 不会被删除,但是当您调用 testGlobal.settings ='test value',因为 remote.getGlobal()只是为您提供了 mainState 的副本,而不是引用。

testGlobal will be garbage collected, since the site changes. global.mainState will not be deleted, however it will also not change when you call testGlobal.settings = 'test value', because remote.getGlobal() just gives you a copy of mainState and not a reference.

我建议您使用 ipcMain ipcRenderer 自己同步全局变量。

I would suggest you use ipcMain and ipcRenderer to sync the global variable yourself.

这篇关于如果渲染器进程关闭,则会收集电子全局变量垃圾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆