electron: Uncaught exception in main process after reloading

This code causes an uncaught exception when focusing the (detached) Developer Tools after reloading the page:

import classnames from 'classnames';
import { remote } from 'electron';

const mainWindow = remote.getCurrentWindow();

mainWindow.on('blur', () => document.body.className = classnames('unfocused'));
mainWindow.on('focus', () => document.body.className = classnames('focused'));

The stack trace:

Uncaught Exception:
Error: Attempting to call a function in a renderer window that has been closed or released. Function provided here: Object.<anonymous> (bootstrap.js:12:12.
    at BrowserWindow.ret (PRIVATE_PATH\node_modules\electron-prebuilt\dist\resources\atom.asar\browser\lib\rpc-server.js:149:21)
    at emitOne (events.js:82:20)
    at BrowserWindow.emit (events.js:169:7)
    at Object.module.exports.showErrorBox (PRIVATE_PATH\node_modules\electron-prebuilt\dist\resources\atom.asar\browser\api\lib\dialog.js:157:35)
    at process.<anonymous> (PRIVATE_PATH\node_modules\electron-prebuilt\dist\resources\atom.asar\browser\lib\init.js:62:19)
    at emitOne (events.js:77:13)
    at process.emit (events.js:169:7)
    at process._fatalException (node.js:224:26)
    at Object.module.exports.showErrorBox (PRIVATE_PATH\node_modules\electron-prebuilt\dist\resources\atom.asar\browser\api\lib\dialog.js:157:35)
    at process.<anonymous> (PRIVATE_PATH\node_modules\electron-prebuilt\dist\resources\atom.asar\browser\lib\init.js:62:19)

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 16 (10 by maintainers)

Most upvoted comments

Method 1: Don’t use events with the remote module (use the ipc module for that) Method 2: Release the callbacks on app startup. Example:

remote.getCurrentWindow().removeAllListeners(); // put this as first line in your code of the renderer-process

Note, that this clears all callbacks, also these you have registred in the main-process! Also note, that you must not click the Developer Tools window while reloading. This would lead to the same crash / error as if not releasing the callbacks.

(for those who want to solve the problem) in renderer process code:

getCurrentWindow().on('blur', titlebarBlur);
getCurrentWindow().on('focus', titlebarFocus);
getCurrentWindow().on('maximize', maxOrRes);
getCurrentWindow().on('restore', maxOrRes);

fix: (add)

window.addEventListener('beforeunload', ()=>{
  getCurrentWindow().removeListener('blur', titlebarBlur);
  getCurrentWindow().removeListener('focus', titlebarFocus);
  getCurrentWindow().removeListener('maximize', maxOrRes);
  getCurrentWindow().removeListener('restore', maxOrRes);
});

Oh sorry. I’m dumb. I don’t release the callbacks before/after reloading.