List entries / section leaks memory, says Instruments

Hello all,

I currently encounter a strange issue in my app which I also can reproduce in a minimal example. Lets say we just have an application with this code:

// ContentView.swift

var body: some View {
    NavigationSplitView {
        List {
            Section("Header") {
                Text("Text")
            }
        }
    } detail: {
    }
}

Running the app just works fine but running it in Instruments results in a memory leak at object NSMutableIndexSet. Why is that?

Taking a more complex example where the view is refreshed based on the result of a function, it leaks even more increasing memory usage. Example code:

var body: some View {
    NavigationSplitView {
        List {
            Section("Header") {
                ForEach(someClass?.functionReturningArrayOfStrings() ?? [String](), id: \.self) { item in
                    Text(item)
                }
            }
        }
    } detail: {

    }
}

Removing either the Sectionor the entries like Textfixes the memory leak, according to Instruments. Is this a bug I stumbled upon or something I misunderstood here?

I also opened a bug report for now, number is: 12388668

Kind regards,

Jan

So it seems like it has to do with the sections. Anytime I click on a section to hide or show it Instruments adds another entry of NSMutableIndexSet to the leak list. If I don't do that and sit there doing nothing, it just shows the gray minus symbol. Did I misuse Section's here? Or is it really a bug?

Hey Jan, thanks for reaching out and filing a helpful feedback!

I was able to reproduce this on macOS Ventura 13.4 using your attached reproducer. I verified that this is a real leak.

It looks like you're using the Section API correctly, so this probably a SwiftUI bug.

This didn't reproduce for me on macOS Sonoma beta, so it may be resolved there. If you're able to try this with the Sonoma beta, please leave a comment in your feedback report to confirm it's fixed.

List entries / section leaks memory, says Instruments
 
 
Q