示例工程 07-project-process-explorer
07-project-process-explorer/app/diagnostics/destroyed-window.cjs
/* eslint-disable @typescript-eslint/no-require-imports */
// 真实报错复现:窗口 destroy 之后继续访问 webContents。
// 运行:pnpm exec electron diagnostics/destroyed-window.cjs
const { app, BrowserWindow } = require("electron");
app.whenReady().then(async () => {
const win = new BrowserWindow({
show: false,
webPreferences: { contextIsolation: true, sandbox: true },
});
await win.loadURL("data:text/html,<h1>diagnostics</h1>");
win.destroy();
try {
// 窗口已销毁,但变量还在;这里会抛出真实错误。
console.log("pid", win.webContents.getOSProcessId());
} catch (error) {
console.error("ERROR:", error && error.message);
console.error(error && error.stack);
} finally {
app.quit();
}
});