From 8c3b2e7b9d9949020a6146f04b28dc0cdd1e3e19 Mon Sep 17 00:00:00 2001 From: Erick Zhao Date: Sat, 30 Nov 2024 21:34:08 -0800 Subject: [PATCH] docs: add section on resource management to base-window.md (#43610) (#44874) Co-authored-by: Niklas Wenzel --- docs/api/base-window.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/docs/api/base-window.md b/docs/api/base-window.md index b3aa07abc319..c6cc7c878bc5 100644 --- a/docs/api/base-window.md +++ b/docs/api/base-window.md @@ -64,6 +64,31 @@ const child = new BaseWindow({ parent, modal: true }) * On Linux the type of modal windows will be changed to `dialog`. * On Linux many desktop environments do not support hiding a modal window. +## Resource management + +When you add a [`WebContentsView`](web-contents-view.md) to a `BaseWindow` and the `BaseWindow` +is closed, the [`webContents`](web-contents.md) of the `WebContentsView` are not destroyed +automatically. + +It is your responsibility to close the `webContents` when you no longer need them, e.g. when +the `BaseWindow` is closed: + +```js +const { BaseWindow, WebContentsView } = require('electron') + +const win = new BaseWindow({ width: 800, height: 600 }) + +const view = new WebContentsView() +win.contentView.addChildView(view) + +win.on('closed', () => { + view.webContents.close() +}) +``` + +Unlike with a [`BrowserWindow`](browser-window.md), if you don't explicitly close the +`webContents`, you'll encounter memory leaks. + ## Class: BaseWindow > Create and control windows.