From ee9b6248bb2ca6eec918a13d5ed5753f6678ee27 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Thu, 4 Dec 2025 11:18:47 +0000 Subject: [PATCH] fix(electron): cleanup child windows by instance instead of name --- electron/electron.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/electron/electron.js b/electron/electron.js index 1e066bfe0..9caceb366 100644 --- a/electron/electron.js +++ b/electron/electron.js @@ -497,6 +497,14 @@ function createChildWindow(args) { const wind = childWindow.find(item => item.name == name); let browser = wind ? wind.browser : null; let isPreload = false; + // 清理已销毁但仍被引用的窗口,避免对失效对象调用方法 + if (browser && browser.isDestroyed && browser.isDestroyed()) { + const index = childWindow.findIndex(item => item.name == name); + if (index > -1) { + childWindow.splice(index, 1); + } + browser = null; + } if (browser) { browser.focus(); if (args.force === false) { @@ -533,7 +541,7 @@ function createChildWindow(args) { options.parent = mainWindow } - if (preloadWindow && Object.keys(webPreferences).length === 0) { + if (preloadWindow && !preloadWindow.isDestroyed?.() && Object.keys(webPreferences).length === 0) { // 使用预加载窗口 browser = preloadWindow; preloadWindow = null; @@ -579,7 +587,7 @@ function createChildWindow(args) { }) browser.on('closed', () => { - const index = childWindow.findIndex(item => item.name == name); + const index = childWindow.findIndex(item => item.browser === browser); if (index > -1) { childWindow.splice(index, 1) }