From a0fdda54fc859ffef809aa9c8cec3e6c7dc9ab09 Mon Sep 17 00:00:00 2001 From: Selyatin Ismet <50295732+Selyatin@users.noreply.github.com> Date: Sat, 3 May 2025 14:00:24 +0300 Subject: [PATCH] Fix Memory Leak --- Sources/MarkdownWebView/MarkdownWebView.swift | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Sources/MarkdownWebView/MarkdownWebView.swift b/Sources/MarkdownWebView/MarkdownWebView.swift index 2338f75..c4f1c27 100644 --- a/Sources/MarkdownWebView/MarkdownWebView.swift +++ b/Sources/MarkdownWebView/MarkdownWebView.swift @@ -36,6 +36,30 @@ import WebKit #elseif os(iOS) public func makeUIView(context: Context) -> CustomWebView { context.coordinator.platformView } #endif + + #if os(macOS) + public static func dismantleNSView(_ nsView: CustomWebView, coordinator: Coordinator) { + let platformView = coordinator.platformView + + platformView.configuration.userContentController.removeScriptMessageHandler( + forName: "sizeChangeHandler") + platformView.configuration.userContentController.removeScriptMessageHandler( + forName: "renderedContentHandler") + platformView.configuration.userContentController.removeScriptMessageHandler( + forName: "copyToPasteboard") + } + #elseif os(iOS) + public static func dismantleUIView(_ uiView: CustomWebView, coordinator: Coordinator) { + let platformView = coordinator.platformView + + platformView.configuration.userContentController.removeScriptMessageHandler( + forName: "sizeChangeHandler") + platformView.configuration.userContentController.removeScriptMessageHandler( + forName: "renderedContentHandler") + platformView.configuration.userContentController.removeScriptMessageHandler( + forName: "copyToPasteboard") + } + #endif func updatePlatformView(_ platformView: CustomWebView, context _: Context) { guard !platformView.isLoading else { return } /// This function might be called when the page is still loading, at which time `window.proxy` is not available yet.