Skip to content
This repository was archived by the owner on Jul 22, 2023. It is now read-only.
This repository was archived by the owner on Jul 22, 2023. It is now read-only.

Clean up Electron win listeners on app reload #17

@AtiqGauri

Description

@AtiqGauri

Using events in renderer process throw error on development console
saying Attempting to call a function in a renderer window that has been closed or released.

win.on('maximize', toggleMaxRestoreButtons);
win.on('unmaximize', toggleMaxRestoreButtons);

This will cause error because you haven't cleaned up correctly and a listener exists on a remote object from a renderer that does not exist any more.

Solution - Avoid using remote or clean up your event handlers on remote objects if you have to use it

Add event remover in your code where you had added events code

window.onbeforeunload = (e) => {
    win.removeAllListeners();
};

This will remove ALL listeners attached to your window before unloading it.

Note - onbeforeunload will be called before page is refreshed or closed.

Screenshot-
Screenshot (146)

Conclusion- update tutorial to adapt to solution

To reproduce-

This is not same as your tutorial but here too I am using win.on events for blur and focus and it throws errors.
https://gist.github.com/AtiqGauri/1cea1c548025faa77f9f29008ca5a5fe#file-main-js-L4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions