Bug 237652

Summary: WKUserContentController.add(_:name:) leads to memory leak even if wrap the object into a weak reference wrapper
Product: WebKit Reporter: Daohan Chong <dh.chong>
Component: JavaScriptCoreAssignee: Nobody <webkit-unassigned>
Status: NEW ---    
Severity: Major CC: beidson, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: Safari 15   
Hardware: All   
OS: iOS 15   
Attachments:
Description Flags
Demo project none

Description Daohan Chong 2022-03-09 06:01:00 PST
From our developer side, adding user script will unexpectedly leak the WKUserScript object even if the WKWebView instance is deallocated.

Proof: https://twitter.com/WildCat_io/status/1501547346416799746

Workaround of this bug: To remove all message handlers when the WKWebView is deallocated: https://github.com/Lakr233/XTerminalUI/blob/3204296353e8874020c834cc70343f73ad4ecdf9/Sources%2FXTerminalUI%2FXTerminalWebViewDelegate.swift#L26 (It can also be put into WKWebView.deinit).

It can be reproduced on both iOS 15 and macOS 12 (catalyst).
Comment 1 Daohan Chong 2022-03-10 06:05:39 PST
Created attachment 454352 [details]
Demo project
Comment 2 Radar WebKit Bug Importer 2022-03-10 10:21:21 PST
<rdar://problem/90106783>
Comment 3 Daohan Chong 2022-03-18 01:49:23 PDT
Another example: https://stackoverflow.com/a/26383032/2949840