SwiftUI Sheet never releases object from memory on iOS 17

I've found a very strange behaviour which looks like a bug. SwiftUI sheet or fullScreenCover do not release objects that were passed to its item: parameter (and to the view builder body by the way, but here is the simplified case).

It works well and memory is releasing on iOS 16 built with both Xcode 14 or 15. (simulators, devices)

Memory is leaking and NOT releasing on iOS 17 built with Xcode 15. (simulator, device 17.0.2)

Any ideas how we can solve this or we have to wait for the bugfix? This is going to be a global memory leak, I am sure most of SwiftUI apps are using classes passed to the sheet or fullScreenCover.

struct SheetView: View {
    @State var sheetVM: SheetVM?
    
    var body: some View {
        Button {
            sheetVM = .init()
        } label: {
            Text("Navigate")
        }
        .sheet(item: $sheetVM) { sheetVM in
            Color.yellow
        }
    }
}

final class SheetVM: ObservableObject, Identifiable {
    @Published var title: String = "Title"

    init() {
        print("SheetVM init")
    }
    
    deinit {
        print("... SheetVM deinit")
    }
}

struct SheetView_Previews: PreviewProvider {
    static var previews: some View {
        SheetView()
    }
}
Post not yet marked as solved Up vote post of bodich Down vote post of bodich
2.3k views
  • Init once @State var sheetVM = SheetVM() the state will remain singular due to the instance and self capture taking place in button
  • Don’t understand what do you mean

Add a Comment

Replies

There are multiple mentions of this problem, we're seeing it as well.

Similar threads (with no solution):

https://developer.apple.com/forums/thread/736239 https://developer.apple.com/forums/thread/729197 https://developer.apple.com/forums/thread/736110

SO questions:

https://stackoverflow.com/questions/77098992/swiftui-sheets-not-correctly-deinitializing-associated-instances https://stackoverflow.com/questions/77153299/swiftui-view-leaks-in-ios-17

Will also add my SO question I've created earlier. Please upvote to bring more attention. Some newbies downvoted and it prevents the question from being found.

https://stackoverflow.com/questions/77227252/swiftui-sheet-never-releases-object-from-memory

seeing the same issue

Apple staff member admitted this bug, so probably it might be fixed soon https://developer.apple.com/forums/thread/737967?answerId=767599022#767599022

  • If you are using the legacy API, i.e., the ObservableObject protocol, why are you defining the object using the @State property wrapper?

  • Instead, consider defining an object using the StateObject property wrapper.

  • I recommend delving into materials on SwiftUI development before publishing questions or code. It will provide you with a better understanding of the concepts.

  • 🙂🙂🙂

I checked and it seemed to be fixed in Xcode 15.1.

I've checked with Xcode 15.2 and the problem remains. Memory leak as soon as I present a sheet or a fullScreen