From d2a94e6b1374809fdcda9bbf41fa16dfd56848c8 Mon Sep 17 00:00:00 2001 From: loc Date: Tue, 17 Sep 2019 12:48:31 -0400 Subject: [PATCH] fix: free screen and window capturers immediately after we're finished with them (#20156) fix #17937, #19908 --- lib/browser/desktop-capturer.ts | 14 +++++++------- shell/browser/api/atom_api_desktop_capturer.cc | 2 ++ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/browser/desktop-capturer.ts b/lib/browser/desktop-capturer.ts index adcba1f86bab..2797b1c9c1ac 100644 --- a/lib/browser/desktop-capturer.ts +++ b/lib/browser/desktop-capturer.ts @@ -19,7 +19,13 @@ export const getSources = (event: Electron.IpcMainEvent, options: ElectronIntern } const getSources = new Promise((resolve, reject) => { + let capturer: ElectronInternal.DesktopCapturer | null = createDesktopCapturer() + const stopRunning = () => { + if (capturer) { + capturer.emit = null + capturer = null + } // Remove from currentlyRunning once we resolve or reject currentlyRunning = currentlyRunning.filter(running => running.options !== options) } @@ -42,19 +48,13 @@ export const getSources = (event: Electron.IpcMainEvent, options: ElectronIntern }))) }) - let capturer: ElectronInternal.DesktopCapturer | null = createDesktopCapturer() - capturer.emit = emitter.emit.bind(emitter) capturer.startHandling(options.captureWindow, options.captureScreen, options.thumbnailSize, options.fetchWindowIcons) // If the WebContents is destroyed before receiving result, just remove the // reference to emit and the capturer itself so that it never dispatches // back to the renderer - event.sender.once('destroyed', () => { - capturer!.emit = null - capturer = null - stopRunning() - }) + event.sender.once('destroyed', () => stopRunning()) }) currentlyRunning.push({ diff --git a/shell/browser/api/atom_api_desktop_capturer.cc b/shell/browser/api/atom_api_desktop_capturer.cc index 9fac86359489..6c1f8f1ba2e7 100644 --- a/shell/browser/api/atom_api_desktop_capturer.cc +++ b/shell/browser/api/atom_api_desktop_capturer.cc @@ -144,6 +144,7 @@ void DesktopCapturer::UpdateSourcesList(DesktopMediaList* list) { } std::move(window_sources.begin(), window_sources.end(), std::back_inserter(captured_sources_)); + window_capturer_.reset(); } if (capture_screen_ && @@ -194,6 +195,7 @@ void DesktopCapturer::UpdateSourcesList(DesktopMediaList* list) { // individual screen support is added. std::move(screen_sources.begin(), screen_sources.end(), std::back_inserter(captured_sources_)); + screen_capturer_.reset(); } if (!capture_window_ && !capture_screen_)