Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: object has been destroyed #183

Merged
merged 1 commit into from
Aug 28, 2024

Conversation

benoitf
Copy link
Contributor

@benoitf benoitf commented Aug 27, 2024

#182 added some changes in v4.0.2

but in our case, when closing the application there is now an error thrown by the code

A JavaScript error occurred in the main process

Uncaught Exception:
TypeError: Object has been destroyed
at WebContents.disposable (packages/main/dist/index.cjs:11962:12)
at Object.onceWrapper (node:events:634:26)
at WebContents.emit (node:events:519:28)

the related code is:

win.webContents.off('destroyed', disposable);

we can't unsubscribe once it's already destroyed

trying to move the unsubscribe outside of the callback itself (I don't know if it was expected to have a self-reference callback)

@benoitf
Copy link
Contributor Author

benoitf commented Aug 27, 2024

To reproduce the issue in your example, use the following code in fixture.js that will destroy the window

(async () => {
	await app.whenReady();

	const window = new BrowserWindow({
		webPreferences: {
			spellcheck: true,
		},
	});

	await window.loadFile(path.join(import.meta.dirname, 'fixture.html'));

	// wait 5seconds, destroy the window
	setTimeout(() => {
		window.destroy();
	}, 5000);
})();

@sindresorhus
Copy link
Owner

// @lovemegowin

@lovemegowin
Copy link
Contributor

lovemegowin commented Aug 28, 2024

We can remove

win.webContents.off('destroyed', disposable);
to quickly fix this error, because we use once.
But it also will cause memory leak in multiple window case.

Could you please do this remove ? @benoitf

@benoitf
Copy link
Contributor Author

benoitf commented Aug 28, 2024

@lovemegowin I've amended the PR

receive an error from webContents saying the object has been destroyed when executing the line
win.webContents.off('destroyed', disposable);

we can't unsubscribe once it's already destroyed

Signed-off-by: Florent Benoit <[email protected]>
@lovemegowin
Copy link
Contributor

lovemegowin commented Aug 28, 2024

I have reviewed the electron events, but didn't found a reasonable timing to do this cleanup yet.
I tested it use win.close(), the destroyed event will trigger before webContent destroyed, but in win.destroy() this case, the webContent will be destroyed before destroyed event emitted.
Any way, I will do more research about it.

@sindresorhus sindresorhus merged commit d2c3c73 into sindresorhus:main Aug 28, 2024
2 checks passed
@benoitf
Copy link
Contributor Author

benoitf commented Aug 28, 2024

thanks @sindresorhus for the quick merge and the new release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants