Here is a very simple code example that reproduces a memory leak when deleting an item in SwiftUI List. This is reproducible on iOS 15 and iOS 16 (at least).
Item
's are never deallocated even if they are deleted from the list.
import SwiftUI
class Item: Identifiable {
let id: String
init(id: String) {
self.id = id
}
deinit {
print("deinit")
}
}
struct ContentView: View {
@State private var data = [
Item(id: "1"),
Item(id: "2"),
Item(id: "3")
]
var body: some View {
List {
ForEach(data) { Text($0.id) }
.onDelete { indexes in
data.remove(at: indexes.first!)
}
}
}
}
This seems like a very basic example so I am wondering if anyone has noticed this or has a workaround?
LazyVStack
and LazyVGrid
exhibit the same behaviour.