Skip to content

Commit

Permalink
fix: memory leak: Added listener to webContents without cleaning up p…
Browse files Browse the repository at this point in the history
…revious listener
  • Loading branch information
nytamin committed Jan 8, 2024
1 parent c741018 commit 6b5ac10
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/helpers/WindowHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,11 @@ export class WindowHelper extends EventEmitter {
await this.window.webContents.insertCSS(`html, body { background-color: ${defaultColor}; }`)
}

this.setupWebContentListeners()

this.window.setTitle(this.title)
this.window.webContents.on('render-process-gone', (event, details) => {
if (details.reason !== 'clean-exit') {
this.status = {
statusCode: StatusCode.ERROR,
message: `Renderer process gone "${this._url}": ${details.reason}, ${details.exitCode}, "${event}"`,
}
}
})
this.window.webContents.setZoomFactor((this.config.zoomFactor ?? 100) / 100)

await this.listenToContentStatuses()

if (this.config.displayDebug) {
await this.displayDebugOverlay()
}
Expand Down Expand Up @@ -303,6 +295,14 @@ export class WindowHelper extends EventEmitter {
return windowUrl
}
}
private handleRenderProcessGone = (event: Electron.Event, details: Electron.RenderProcessGoneDetails): void => {
if (details.reason !== 'clean-exit') {
this.status = {
statusCode: StatusCode.ERROR,
message: `Renderer process gone "${this._url}": ${details.reason}, ${details.exitCode}, "${event}"`,
}
}
}
private handleConsoleMessage = (
_event: Electron.Event,
level: number,
Expand All @@ -316,7 +316,10 @@ export class WindowHelper extends EventEmitter {
}
}

private async listenToContentStatuses() {
private setupWebContentListeners() {
this.window.webContents.off('render-process-gone', this.handleRenderProcessGone)
this.window.webContents.on('render-process-gone', this.handleRenderProcessGone)

this.window.webContents.off('console-message', this.handleConsoleMessage)
this.window.webContents.on('console-message', this.handleConsoleMessage)
}
Expand Down

0 comments on commit 6b5ac10

Please sign in to comment.