Skip to content

Fix "Object has been destroyed" error when BrowserView is disposed after window close#312798

Merged
kycutler merged 2 commits into
mainfrom
copilot/fix-unhandled-error-object-destroyed
Apr 27, 2026
Merged

Fix "Object has been destroyed" error when BrowserView is disposed after window close#312798
kycutler merged 2 commits into
mainfrom
copilot/fix-unhandled-error-object-destroyed

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 27, 2026

When an Electron BrowserWindow fires 'closed', the window object is already destroyed before any listeners run. BrowserView listens to onDidClose and calls dispose(), which called contentView.removeChildView() on the now-destroyed BrowserWindow — throwing "Object has been destroyed".

Changes

  • browserView.tsdispose(): Guard removeChildView with !win.isDestroyed(), consistent with the existing isDestroyed() check already present for webContents.close() a few lines below.
// Before
this._currentWindow?.win?.contentView.removeChildView(this._view);

// After
const currentWin = this._currentWindow?.win;
if (currentWin && !currentWin.isDestroyed()) {
    currentWin.contentView.removeChildView(this._view);
}

The optional chaining ?.win only guards null/undefined — it does not protect against a valid-but-destroyed BrowserWindow reference, which is the state after 'closed' fires.

Copilot AI self-assigned this Apr 27, 2026
Copilot AI review requested due to automatic review settings April 27, 2026 15:38
Copilot AI review requested due to automatic review settings April 27, 2026 15:38
Copilot AI linked an issue Apr 27, 2026 that may be closed by this pull request
… window close

Agent-Logs-Url: https://github.com/microsoft/vscode/sessions/68428537-094f-4b19-8960-68dd9f651e62

Co-authored-by: kycutler <67761731+kycutler@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot April 27, 2026 15:43
Copilot AI changed the title [WIP] Fix unhandled error when object has been destroyed Fix "Object has been destroyed" error when BrowserView is disposed after window close Apr 27, 2026
Copilot AI requested a review from kycutler April 27, 2026 15:44
@kycutler kycutler marked this pull request as ready for review April 27, 2026 16:15
Copilot AI review requested due to automatic review settings April 27, 2026 16:15
@kycutler kycutler enabled auto-merge (squash) April 27, 2026 16:15
@vs-code-engineering
Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

@jruales

Matched files:

  • src/vs/platform/browserView/electron-main/browserView.ts

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an Electron main-process crash/exception caused by disposing a BrowserView after its hosting BrowserWindow has already been destroyed (a known state by the time the 'closed' event listeners run). The change makes BrowserView.dispose() resilient to destroyed-window references.

Changes:

  • Add an isDestroyed() guard before calling BrowserWindow.contentView.removeChildView(...) during BrowserView.dispose().
  • Keep existing webContents.isDestroyed() guard behavior intact for webContents.close(...).
Show a summary per file
File Description
src/vs/platform/browserView/electron-main/browserView.ts Prevents "Object has been destroyed" by skipping removeChildView when the hosting window is already destroyed during disposal.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 0

@kycutler kycutler merged commit 54d9b1f into main Apr 27, 2026
44 of 45 checks passed
@kycutler kycutler deleted the copilot/fix-unhandled-error-object-destroyed branch April 27, 2026 16:54
@vs-code-engineering vs-code-engineering Bot added this to the 1.119.0 milestone Apr 27, 2026
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.

[Error] unhandlederror-Object has been destroyed

4 participants