0

I'm initiating a new BrowserWindow when the menu item is clicked from a Tray instance. Whenever the window is closed, it also quits the whole app including the Tray instance. To overcome that I'm using below code :

click: function () 
{
    var win = new BrowserWindow({
        width: 400,
        height: 600,
        resizable: false,
        fullscreen: false,
        title: 'About',
        icon : __dirname+'/assets/logo/windowIcon.png'
    })
    win.setMenu(null)
    win.loadURL(`file://${__dirname}/about.html`)
    // win.webContents.openDevTools()

    win.on('close', function (e) 
    {
        e.preventDefault()
        win.hide() 
        win.removeAllListeners('close')
    })
}

But later I found that even after the window is closed the BrowserWindow instance is not discarded/release from the memory.

Memory Leak

Every time that menu is clicked it opens a new Window but upon closing it, it is not released from the memory and the process just stacks up into the memory.

How do I release the BrowserWindow instance from the memory without terminating the Tray instance ?

Vaibhav Sidapara
  • 159
  • 2
  • 12

2 Answers2

0

What does your main process do when all the windows are closed? Here's what the Electron Quick Start does:

// Quit when all windows are closed.
app.on('window-all-closed', function () {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})
Vadim Macagon
  • 14,463
  • 2
  • 52
  • 45
  • Hi Vadim, I do not want to quit the app I just want to clear the BrowerWindow instance from the memory. Right now I'm using 'e.preventDefault(); win.hide();' when the window is closed and I think since I'm hiding the window it stays in the memory. When I use 'win.close()' it also quits the app. So my question how can I close the window without the quitting the app and discarding the window instance from memory as well. – Vaibhav Sidapara Sep 22 '16 at 01:30
  • @VaibhavSidapara my point was that you need to handle the [`window-all-closed`](http://electron.atom.io/docs/api/app/#event-window-all-closed) event. – Vadim Macagon Sep 22 '16 at 05:55
  • thank you for the suggestion, but my app is just a tray app if I put that logic will it also quit the tray app ? The window is just an about us page nothing else. The whole app runs as a tray application. – Vaibhav Sidapara Sep 22 '16 at 06:55
0

By calling preventDefault() on the event, you're preventing the window from being destroyed in the main process, so it gets leaked.

Instead of creating a new window on each click, you can define a module-level reference to the window and only create it when the menu item is clicked the first time:

// Module scope
let win = null;

// ... your menu code

click: function () {
    if(win === null) {
        win = new BrowserWindow({
            width: 400,
            height: 600,
            resizable: false,
            fullscreen: false,
            title: 'About',
            icon : __dirname+'/assets/logo/windowIcon.png'
        });

        win.setMenu(null);
        win.loadURL(`file://${__dirname}/about.html`);

        win.on('close', function (e) {
            e.preventDefault();
            win.hide();
        });
    } else {
        win.show();
    }
}

However, as Vadim Macagon pointed out, it looks like you have a 'window-all-closed' event defined on the application in which it is told to quit when all windows are closed. Do a search on all your files for 'window-all-closed' and remove the app.quit() call. If you do that, you should be able to leave your code as is.

krono
  • 21
  • 5