Object has been destroyed when open secondary/child window


In a main window, when a button is clicked, the second/child window will popped up via an ipc call.
This works when open the pop window on the first time. If I closed the pop window and reopen it again, I will get this error:

Uncaught Exception:
Error: Object has been destroyed
at Error (native)
at EventEmitter. (/home/xxxx/electron/fin/main.js:36:21)
at emitOne (events.js:96:13)
at EventEmitter.emit (events.js:188:7)
at EventEmitter. (/home/xxxx/electron/fin/node_modules/electron-prebuilt/dist/resources/electron.asar/browser/api/web-contents.js:156:13)
at emitTwo (events.js:106:13)
at EventEmitter.emit (events.js:191:7)

Here is my main.js codes that create the windows:

mainWindow = new BrowserWindow({width: 800, height: 600})



  mainWindow.on('closed', function () {
    mainWindow = null

  let popWindow = new BrowserWindow({parent: mainWindow, width: 450, height: 450, show: false});
  ipc.on('toggle-popwindow', function(){
      console.log('call pop window ipc toggle')



If I add the hide() in the ‘close’ method to the popwindow, I will get this error:
popWindow.on(‘closed’, function (event) {

Uncaught Exception:
Error: Object has been destroyed

What is the problem?


Is this a bug? I couldn’t find any solution/related topics on this issue.


If you close a browser window it will be destroyed, so you can’t hide or show it again after that. Since you want to hide it and show it again later your should add a listener for the close event that calls preventDefault() and hides the window instead of closing it.


Yes. You are right. It is ‘close’ event instead of ‘closed’ event. It was overlooked by me.
For those has similar problem:

popWindow.on('close', function (event) {

Close a sub-window

thank you … very much