SwiftUI: @StateObject never deinitialized

I am using @StateObject for creating a view model for my view. The problem is that the ViewModel is still in memory after closing the view and deinit is never called. If I open and close the view 10 times, the debug memory graph shows me 10 ViewModels. I created a very simple example to replicate the problem:

import SwiftUI

struct MySwiftUIView: View {
    @Environment(\.presentationMode) private var presentation
    @StateObject var viewModel = MyViewModel()

        var body: some View {
                Text("Hallo World.")
                    .navigationBarBackButtonHidden(true)
                    .navigationBarItems(leading: Button(
                        action: {
                            presentation.wrappedValue.dismiss()
                    },
                        label: { Text("back") }))
            }
    }

class MyViewModel: ObservableObject {
    init() {
        print(">> init")
    }

    deinit {
        print("[x] deinit")
    }
}

">> init" is printed every time when I am opening the view, but "[x] deinit" is never printed. This is how I am opening the view:

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            VStack{
                NavigationLink(destination: MySwiftUIView()){Text("NavigationLink")}
            }
        }.navigationViewStyle(StackNavigationViewStyle())
    }
}

Does anybody have an idea why the ViewModel never gets destroyed?

I created a demo project in Xcode which can be found here: https://file.io/QQnHDvRNaGPP

edit: It works with the newest iOS15 Beta, but not with iOS14.5 or lower.

In SwiftUI we don't use view model objects. The View structs (or custom @State structs) hold the data that SwiftUI uses to diff and update the screen. View models are a UIKit pattern don't use it in SwiftUI, if you do then you'll add an unnecessary level of indirection and slow SwiftUI down and lose a lot of the magic behaviour.

⚠️ do not forget to add .navigationViewStyle(.stack) to Navigation View. It will deinit() the view model ⚠️ But it's also important to kill all objects in the ViewModel to clear the memory graph. When you use Combine for example)) Without it, the Debug Navigator will tell you about warnings.

Add to ViewModel: `public func invalidate() {     cancellable?.cancel()     cancellable = nil     print("[<<] invalidated")   }

And call in MySwiftUIView(): ` .onDisappear {       vm.invalidate()     }

Use NavigationStack instead of NavigationView, it works.

SwiftUI: @StateObject never deinitialized
 
 
Q