I have something like this:
struct SomeView: View {
@ObservedObject var viewModel: SomeViewModel
var body: some View {
NavigationView { // <- culprit
Button(action: { self.viewModel.logOut() }) { Text("X").frame(width: 40, height: 40) }
}
}
class SomeViewModel: ObservableObject {
func logOut() {
// changes global state, based on which the views are swapped, so `SomeView` is removed and replaced by a different one
}
}
When the button is pressed, SomeView
is closed and a different view is presented. But if I check the memory graph, SomeViewModel
is still allocated because self.viewModel.logOut()
is called in the Button's action closure and Button is holding the reference to SomeViewModel
.
Is there some way around this?
EDIT:
Actually, when not not wrapping the button in NavigationView
, there is no leak. As soon as I wrap the button, the leak appears. Wrapping in VStack
is working out fine. But wrapping in Form
produces the leak again. Seems like the same problem here: SwiftUI - Possible Memory Leak