Q:

SwiftUI Memory Leak With Forms and Lists

I am developing simple to-do app with SwiftUI. During my tests i noticed my view models never calls deinit and causing linear increase in memory usage.
I reproduced the same behavior with following code:
struct ContentView: View {
    @State private var isPresented = false

    var body: some View {
        Button("open") {
            self.isPresented = true
        }
        .sheet(isPresented: $isPresented) {
            SheetView()
        }
    }
}

struct SheetView: View {
    @ObservedObject var model: ViewModel

    init() {
        model = ViewModel()
    }

    var body: some View {
        Form {
            Toggle("Toggle Me", isOn: $model.isOn)
        }
    }
}

class ViewModel: ObservableObject {
    @Published var isOn = false

    deinit {
        print("ViewModel deinit ")
    }
}

When sheet is dismissed, model object never deinits. If i replace the form with VStack or ScrollView then model is deinited. Is there a solution to this?

A:

It’s a bug. Only workaround that worked for me is using ScrollView. Then again, ScrollView comes with its own animation bugs.
EDIT
Issue seems to have been resolved in iOS 13.3.1

