Skip to content

Commit 18fe31c

Browse files
committed
Fix memory captchaleak
1 parent 082a04a commit 18fe31c

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Flat/Modules/Login/CaptchaWebViewController.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ final class CaptchaWebViewController: UIViewController {
2626
private let captchaPrefix: String
2727
private let captchaSceneId: String
2828
private var hasStoppedLoading = false
29+
private var didCleanupHandlers = false
2930

3031
private var presentCloseButton: UIButton?
3132
private var presentTitleLabel: UILabel?
@@ -53,6 +54,17 @@ final class CaptchaWebViewController: UIViewController {
5354
loadCaptchaHTML()
5455
}
5556

57+
override func viewDidDisappear(_ animated: Bool) {
58+
super.viewDidDisappear(animated)
59+
if isBeingDismissed || isMovingFromParent || view.window == nil {
60+
cleanupHandlersIfNeeded()
61+
}
62+
}
63+
64+
deinit {
65+
cleanupHandlersIfNeeded()
66+
}
67+
5668
override func viewWillAppear(_ animated: Bool) {
5769
super.viewWillAppear(animated)
5870

@@ -180,6 +192,15 @@ extension CaptchaWebViewController: WKScriptMessageHandler {
180192
}
181193

182194
private extension CaptchaWebViewController {
195+
func cleanupHandlersIfNeeded() {
196+
guard !didCleanupHandlers else { return }
197+
didCleanupHandlers = true
198+
let controller = webView.configuration.userContentController
199+
controller.removeScriptMessageHandler(forName: Constants.messageHandlerName)
200+
controller.removeScriptMessageHandler(forName: Constants.readyMessageHandlerName)
201+
webView.navigationDelegate = nil
202+
}
203+
183204
func stopLoadingOnce() {
184205
guard !hasStoppedLoading else { return }
185206
hasStoppedLoading = true

0 commit comments

Comments
 (0)