WatchOS 10.5 TabView Mem Leak Help Please

Target: WatchOS 10.5

NOTE: This is a watchOS only app


Given:

A single view containing NavigationSplitview, with the List in the "sidebar", a TabView in the "detail" and a TabView in a sheet attached to each tab in the "detail" view.

When:

Navigating between top-level list and "detail" TabView, or navigating through "detail" to "sheet" TabView

Then:

Memory leaks occur.


If the TabView() views are replaced with List() views there are no longer memory leaks.

There are no reference types involved. Everything is in Structs

Code below causes the issue which can be observed in Instruments.

So my question is what have I coded incorrectly to cause this issue? Or, How can I fix this?

Thanks in advance.


@main
struct VerticalTabView_MemLeak: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

struct ContentView: View {
    struct ParentItem: Identifiable, Hashable {
        var id = UUID()
        var name: String
        var children: [Item]
        init(_ name: String, _ children: [Item]){
            self.name = name
            self.children = children
        }
    }

    struct Item: Identifiable, Hashable {
        var id: UUID = UUID()
        var name: String
        init(_ name: String){ self.name = name }
    }

    @State var selectedParentItem: ParentItem?
    @State var selectedItem: Item?

    var parentItems = [
        ParentItem("A", [Item("one"),Item("two"),Item("three")]),
        ParentItem("B", [Item("four"),Item("five"),Item("six")]),
        ParentItem("C", [Item("seven"),Item("eight"),Item("nine")])
    ]

    var body: some View {
        NavigationSplitView {
            List(selection: $selectedParentItem) {
                ForEach(parentItems, id: \.id) { parentItem in
                    NavigationLink(value: parentItem) {
                        HStack {
                            Text(parentItem.name)
                        }
                        .padding()
                    }
                }
            }
            .navigationTitle("Top Level")
        } detail: {
            if let items = selectedParentItem?.children {
                TabView(selection: $selectedItem) {
                    ForEach(items, id:\.id) { item in
                        Text(verbatim: item.name)
                            .tag(item)
                            .onTapGesture {
                                selectedItem = item
                            }
                    }
                }
                .tabViewStyle(.verticalPage)
                .navigationTitle(selectedParentItem?.name ?? "")
                .sheet(item: $selectedItem,
                       onDismiss: { selectedItem = nil },
                       content: { item in
                    TabView {
                        Text(item.name).foregroundStyle(.yellow)
                        Text(item.name).foregroundStyle(.yellow)
                    }
                    .tabViewStyle(.verticalPage)
                    .navigationTitle(selectedParentItem?.name ?? "")
                })
            }
        }
    }
}

#Preview {
    ContentView()
}
Answered by DTS Engineer in 795419022

Please file a bug report using Feedback Assistant. Thanks!

Accepted Answer

Please file a bug report using Feedback Assistant. Thanks!

WatchOS 10.5 TabView Mem Leak Help Please
 
 
Q