Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It was necessary to have strong references to WebViewConfiguration, UIDelegate and NavigationDelegate in WebView. #42

Closed
Kyome22 opened this issue Jan 21, 2025 · 1 comment · Fixed by #43

Comments

@Kyome22
Copy link
Contributor

Kyome22 commented Jan 21, 2025

We changed the strong reference to a weak reference in PR #40, but this was a mistake.
The following implementations will no longer function.

WebView(configuration: MyWebViewConfiguration())
    .uiDelegate(MyUIDelegate())
    .navigationDelegate(MyNavigationDelegate())

The strong references to MyWebViewConfiguration, MyUIDelegate, and MyNavigationDelegate are nowhere to be found, so the instances are destroyed before they are applied to the WKWebView.

It will work if the parent View of the WebView has these references, but it is not necessarily used that way.

@Kyome22
Copy link
Contributor Author

Kyome22 commented Jan 21, 2025

Even if ChildView has a reference to ParentView, that alone will not cause a memory leak, so perhaps it is not a problem if it has a reference to an instance in struct View.

import SwiftUI

@main
struct TestAppApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

struct ContentView: View {
    @State var isOn = false
    
    var body: some View {
        VStack {
            Toggle(isOn: $isOn) {
                Text("Open")
            }
        }
        .sheet(isPresented: $isOn) {
            ParentView()
        }
    }
}

struct ParentView: View {
    var body: some View {
        VStack {
            Text("ParentView")
            ChildView(parent: self)
        }
    }
}

struct ChildView: View {
    var parent: ParentView
    
    var body: some View {
        Text("ChildView")
    }
}

@Kyome22 Kyome22 mentioned this issue Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant