Electron object destroyed

154 Views Asked by At

when I close my electron app sometimes I get an error object destroyed blabla. I googled it and I don't really think it's a proper way to fix it... Someone advised to fix it like this

ipcMain.on('aMessage', async e => {
    
    // do smth with a message and then send the answer


    //next is the fix... really?
    try {
        win.webContents.send('position', res.data);
    } catch (error) {
        console.log(error);
    }
});

so the proposed fix is just to wrap a sending lines into a try catch block... It just cannot be as it meant... Please tell how to properly fix object destroyed electron error. thank you

1

There are 1 best solutions below

0
Zero Trick Pony On

Your code seems to have a race condition where the main window has already been destroyed before you send the "position" IPC to it. Presuming that you are getting TypeError: Object has been destroyed, you can check for whether the window is still around before sending the IPC. For example:

ipcMain.on('aMessage', async e => {
  if (!win.isDestroyed()) {
    win.webContents.send('position', res.data);
  }
});