Skip to content

Commit 1c89d4e

Browse files
committed
[desktop_webview_window] fix memory leak on macOS after close window
1 parent a8e2bab commit 1c89d4e

File tree

9 files changed

+51
-89
lines changed

9 files changed

+51
-89
lines changed

packages/desktop_webview_window/example/macos/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ EXTERNAL SOURCES:
2020

2121
SPEC CHECKSUMS:
2222
desktop_webview_window: d4365e71bcd4e1aa0c14cf0377aa24db0c16a7e2
23-
FlutterMacOS: 57701585bf7de1b3fc2bb61f6378d73bbdea8424
24-
path_provider_macos: 160cab0d5461f0c0e02995469a98f24bdb9a3f1f
23+
FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24
24+
path_provider_macos: 3c0c3b4b0d4a76d2bf989a913c2de869c5641a19
2525

2626
PODFILE CHECKSUM: c7161fcf45d4fd9025dc0f48a76d6e64e52f8176
2727

28-
COCOAPODS: 1.11.2
28+
COCOAPODS: 1.12.1

packages/desktop_webview_window/example/macos/Runner.xcodeproj/project.pbxproj

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
archiveVersion = 1;
44
classes = {
55
};
6-
objectVersion = 51;
6+
objectVersion = 54;
77
objects = {
88

99
/* Begin PBXAggregateTarget section */
@@ -202,7 +202,7 @@
202202
isa = PBXProject;
203203
attributes = {
204204
LastSwiftUpdateCheck = 0920;
205-
LastUpgradeCheck = 1300;
205+
LastUpgradeCheck = 1430;
206206
ORGANIZATIONNAME = "";
207207
TargetAttributes = {
208208
33CC10EC2044A3C60003C045 = {
@@ -255,6 +255,7 @@
255255
/* Begin PBXShellScriptBuildPhase section */
256256
3399D490228B24CF009A79C7 /* ShellScript */ = {
257257
isa = PBXShellScriptBuildPhase;
258+
alwaysOutOfDate = 1;
258259
buildActionMask = 2147483647;
259260
files = (
260261
);
@@ -403,7 +404,7 @@
403404
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
404405
GCC_WARN_UNUSED_FUNCTION = YES;
405406
GCC_WARN_UNUSED_VARIABLE = YES;
406-
MACOSX_DEPLOYMENT_TARGET = 10.11;
407+
MACOSX_DEPLOYMENT_TARGET = 10.14;
407408
MTL_ENABLE_DEBUG_INFO = NO;
408409
SDKROOT = macosx;
409410
SWIFT_COMPILATION_MODE = wholemodule;
@@ -483,7 +484,7 @@
483484
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
484485
GCC_WARN_UNUSED_FUNCTION = YES;
485486
GCC_WARN_UNUSED_VARIABLE = YES;
486-
MACOSX_DEPLOYMENT_TARGET = 10.11;
487+
MACOSX_DEPLOYMENT_TARGET = 10.14;
487488
MTL_ENABLE_DEBUG_INFO = YES;
488489
ONLY_ACTIVE_ARCH = YES;
489490
SDKROOT = macosx;
@@ -530,7 +531,7 @@
530531
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
531532
GCC_WARN_UNUSED_FUNCTION = YES;
532533
GCC_WARN_UNUSED_VARIABLE = YES;
533-
MACOSX_DEPLOYMENT_TARGET = 10.11;
534+
MACOSX_DEPLOYMENT_TARGET = 10.14;
534535
MTL_ENABLE_DEBUG_INFO = NO;
535536
SDKROOT = macosx;
536537
SWIFT_COMPILATION_MODE = wholemodule;

packages/desktop_webview_window/example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1300"
3+
LastUpgradeVersion = "1430"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

packages/desktop_webview_window/example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ packages:
5555
path: ".."
5656
relative: true
5757
source: path
58-
version: "0.2.0"
58+
version: "0.2.1"
5959
fake_async:
6060
dependency: transitive
6161
description:

packages/desktop_webview_window/macos/Classes/DesktopWebviewWindowPlugin.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,6 @@ public class DesktopWebviewWindowPlugin: NSObject, FlutterPlugin {
278278
}
279279

280280
func onWebviewWindowClose(viewId: Int64, wc: WebviewWindowController) {
281-
wc.destroy()
282-
let wc = webviews.removeValue(forKey: viewId)
283-
wc?.window?.windowController = nil
284-
wc?.window = nil
281+
webviews.removeValue(forKey: viewId)
285282
}
286283
}

packages/desktop_webview_window/macos/Classes/WebViewLayoutController.swift

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class WebViewLayoutController: NSViewController {
106106
"id": viewId,
107107
"canGoBack": webView.canGoBack,
108108
"canGoForward": webView.canGoForward,
109-
])
109+
] as [String: Any])
110110
} else if keyPath == "loading" {
111111
if webView.isLoading {
112112
methodChannel.invokeMethod("onNavigationStarted", arguments: [
@@ -147,16 +147,9 @@ class WebViewLayoutController: NSViewController {
147147
}
148148

149149
func destroy() {
150-
webView.removeObserver(self, forKeyPath: "canGoBack")
151-
webView.removeObserver(self, forKeyPath: "canGoForward")
152-
webView.removeObserver(self, forKeyPath: "loading")
153-
154-
webView.uiDelegate = nil
155-
webView.navigationDelegate = nil
156-
javaScriptHandlerNames.forEach { name in
157-
webView.configuration.userContentController.removeScriptMessageHandler(forName: name)
158-
}
159-
webView.configuration.userContentController.removeAllUserScripts()
150+
webView.stopLoading(self)
151+
webView.removeFromSuperview()
152+
titleBarController.engine.shutDownEngine()
160153
}
161154

162155
func reload() {
@@ -188,6 +181,12 @@ class WebViewLayoutController: NSViewController {
188181
completer(result)
189182
}
190183
}
184+
185+
deinit {
186+
#if DEBUG
187+
print("\(self) deinited")
188+
#endif
189+
}
191190
}
192191

193192
extension WebViewLayoutController: WKNavigationDelegate {
@@ -205,7 +204,7 @@ extension WebViewLayoutController: WKNavigationDelegate {
205204
methodChannel.invokeMethod("onUrlRequested", arguments: [
206205
"id": viewId,
207206
"url": url.absoluteString,
208-
])
207+
] as [String: Any])
209208

210209
decisionHandler(.allow)
211210
}
@@ -223,9 +222,9 @@ extension WebViewLayoutController: WKUIDelegate {
223222
"id": viewId,
224223
"prompt": prompt,
225224
"defaultText": defaultText ?? "",
226-
]) { result in
227-
completionHandler((result as? String) ?? "")
228-
}
225+
] as [String: Any]) { result in
226+
completionHandler((result as? String) ?? "")
227+
}
229228
}
230229

231230
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {

packages/desktop_webview_window/macos/Classes/WebViewLayoutController.xib

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="19455" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="21701" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
33
<dependencies>
44
<deployment identifier="macosx"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="19455"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21701"/>
66
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
77
</dependencies>
88
<objects>

packages/desktop_webview_window/macos/Classes/WebviewWindowController.swift

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ class WebviewWindowController: NSWindowController {
1717
private let width, height: Int
1818

1919
private let titleBarHeight: Int
20-
20+
2121
private let titleBarTopPadding: Int
22-
22+
2323
private let title: String
24-
24+
2525
public weak var webviewPlugin: DesktopWebviewWindowPlugin?
2626

2727
init(viewId: Int64, methodChannel: FlutterMethodChannel,
@@ -36,30 +36,28 @@ class WebviewWindowController: NSWindowController {
3636
self.titleBarTopPadding = titleBarTopPadding
3737
self.title = title
3838
super.init(window: nil)
39-
}
40-
41-
required init?(coder: NSCoder) {
42-
fatalError("init(coder:) has not been implemented")
43-
}
44-
45-
public var webViewController: WebViewLayoutController {
46-
contentViewController as! WebViewLayoutController
47-
}
4839

49-
override func windowDidLoad() {
50-
super.windowDidLoad()
40+
let newWindow = NSWindow(contentRect: NSRect(x: 0, y: 0, width: width, height: height), styleMask: [.titled, .closable, .resizable, .fullSizeContentView], backing: .buffered, defer: false)
41+
newWindow.delegate = self
42+
newWindow.title = title
43+
newWindow.titlebarAppearsTransparent = true
44+
newWindow.center()
5145

52-
contentViewController = WebViewLayoutController(
46+
let contentViewController = WebViewLayoutController(
5347
methodChannel: methodChannel,
5448
viewId: viewId, titleBarHeight: titleBarHeight,
5549
titleBarTopPadding: titleBarTopPadding)
50+
newWindow.contentViewController = contentViewController
5651

57-
window?.setContentSize(NSSize(width: width, height: height))
58-
window?.center()
59-
window?.title = title
52+
window = newWindow
53+
}
54+
55+
required init?(coder: NSCoder) {
56+
fatalError("init(coder:) has not been implemented")
57+
}
6058

61-
window?.isReleasedWhenClosed = false
62-
window?.delegate = self
59+
public var webViewController: WebViewLayoutController {
60+
window?.contentViewController as! WebViewLayoutController
6361
}
6462

6563
override func keyDown(with event: NSEvent) {
@@ -69,8 +67,10 @@ class WebviewWindowController: NSWindowController {
6967
}
7068

7169
func destroy() {
70+
webViewController.destroy()
71+
webviewPlugin = nil
7272
window?.delegate = nil
73-
contentViewController = nil
73+
window = nil
7474
}
7575

7676
func setAppearance(brightness: Int) {
@@ -96,18 +96,13 @@ class WebviewWindowController: NSWindowController {
9696
print("\(self) deinited")
9797
#endif
9898
}
99-
100-
override var windowNibName: NSNib.Name? {
101-
"WebviewWindowController"
102-
}
10399
}
104100

105101
extension WebviewWindowController: NSWindowDelegate {
106102
func windowWillClose(_ notification: Notification) {
107103
webViewController.destroy()
108104
methodChannel.invokeMethod("onWindowClose", arguments: ["id": viewId])
109-
DispatchQueue.main.async {
110-
self.webviewPlugin?.onWebviewWindowClose(viewId: self.viewId, wc: self)
111-
}
105+
webviewPlugin?.onWebviewWindowClose(viewId: viewId, wc: self)
106+
destroy()
112107
}
113108
}

packages/desktop_webview_window/macos/Classes/WebviewWindowController.xib

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)