0

I'm aware about this issue that UIViewControllerRepresentable could cause a memory leak. Even if it should be fixed with past Xcode releases, I'm facing it only when embedding it in a NavigationView. I'm using Xcode Version 11.7 (11E801a) on a physical iPhone 11 iOS 13.7

Here an example:

Memory leak

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {   // Memory leak
            ZStack {
                Text("Hello, World!")
                ViewControllerContainer() // Memory leak
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


struct ViewControllerContainer: UIViewControllerRepresentable {
    
    func makeUIViewController(context: Context) -> UIViewController {
        return UIViewController()
    }
    
    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
        
    }
}

No memory leak

import SwiftUI

struct ContentView: View {
    var body: some View {
        ZStack {
            Text("Hello, World!")
            ViewControllerContainer() // No memory leak
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


struct ViewControllerContainer: UIViewControllerRepresentable {
    
    func makeUIViewController(context: Context) -> UIViewController {
        return UIViewController()
    }
    
    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
        
    }
}

Is it a xcode/swiftui related bug or I missing something?

EDIT: Screenshot of Instruments/Leaks enter image description here

UPDATE:

The leak is not showing up with iPhone 11 (iOS 13.7) simulator

JohnnyParafango
  • 316
  • 2
  • 11

0 Answers0