electron-trpc
electron-trpc copied to clipboard
App crashes with "Object has been destroyed" error
I believe it is coming from here due to win
being destroyed:
https://github.com/jsonnull/electron-trpc/blob/8c2de955b2256e460d827d2103727b03807d00cf/packages/electron-trpc/src/main/createIPCHandler.ts#L57
Here's the full error:
Hey @thecodrr, can you provide more information about when the error occurs?
I'm confident I have a fix, but I want to be sure I understand the root cause before I patch it.
@jsonnull this occurs on closing the window.
Did you figure out a solution? Running into this as well.
TypeError: Object has been destroyed
at Q.detachWindow (/Users/joshualipson/GitHub/medal/node_modules/electron-trpc/dist/main.cjs:1:3867)
at WebContents.<anonymous> (/Users/joshualipson/GitHub/medal/node_modules/electron-trpc/dist/main.cjs:1:4021)
at WebContents.emit (node:events:529:35)
For us, this was happening because our app explicitly calls destroy
on a BrowserWindow.
Reproduction
You can reproduce it by calling win.destroy()
in the ready
event handler in the example here: https://github.com/jsonnull/electron-trpc/blob/cfdb2e58126deb5867d57e21b5aac1d96ef49b44/examples/basic-react/electron/index.ts#L19
For example:
setTimeout(() => {
win.destroy()
}, 5000)
I haven't found a workaround other than to avoid calling destroy()
.
Root Cause
detachWindow
is called in win.webContents.on('destroyed', () => {
: https://github.com/jsonnull/electron-trpc/blob/cfdb2e58126deb5867d57e21b5aac1d96ef49b44/packages/electron-trpc/src/main/createIPCHandler.ts#L95
detachWindow
then accesses win.webContents.id
, which throws an error if the window is already destroyed: https://github.com/jsonnull/electron-trpc/blob/cfdb2e58126deb5867d57e21b5aac1d96ef49b44/packages/electron-trpc/src/main/createIPCHandler.ts#L62
It also accesses win.id
, which used to throw an error, but now does not: https://github.com/electron/electron/issues/38170
Apparently the destroyed
event doesn't guarantee that the window hasn't yet been destroyed.
Potential Fix
The easiest would probably be to capture win.webContents.id
earlier in attachSubscriptionCleanupHandlers
and pass it to detachWindow
: https://github.com/jsonnull/electron-trpc/blob/cfdb2e58126deb5867d57e21b5aac1d96ef49b44/packages/electron-trpc/src/main/createIPCHandler.ts#L81-L98
I'll put up a PR with this fix, but I'm definitely open to alternatives!